From 837b6bbbbd0c7b6ccd1868b1d37cecc740adb1e5 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sun, 28 Jan 2024 00:20:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E4=B8=AA=E5=A4=A7=E7=9A=84=EF=BC=8C?= =?UTF-8?q?=E7=BB=A7=E7=BB=AD=E6=94=B6=E6=8B=BE=E4=B8=9C=E8=A5=BF=E5=8E=BB?= =?UTF-8?q?=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 1 + src/cluster.rs | 12 +++++++++--- src/config.rs | 45 ++++++++++++++++++++++++++++----------------- src/log.rs | 23 ++++++++++++++++++++++- src/main.rs | 4 +++- src/utils.rs | 17 ++++++++++++++--- 6 files changed, 77 insertions(+), 25 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8797702..a4bd084 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ sha1 = "0.10.6" zstd = "0.13.0" log = "0.4.20" +simple_logger = "4.3.3" # [dependencies.db_logger] # version = "0.1" # optional = true diff --git a/src/cluster.rs b/src/cluster.rs index 7fab1fa..ca91f32 100644 --- a/src/cluster.rs +++ b/src/cluster.rs @@ -1,4 +1,4 @@ -use crate::config::{Config}; +use crate::config::Config; use crate::utils::avro_data_to_file_list; use crate::PROTOCOL_VERSION; @@ -63,7 +63,11 @@ impl Cluster { let url = self.config.join_center_url("/openbmclapi/files"); let password = self.config.cluster_secret.clone(); let username = self.config.cluster_id.clone(); - let client = Client::builder().user_agent(self.ua.clone()).build().unwrap(); + let client = Client::builder() + .user_agent(self.ua.clone()) + .build() + .unwrap(); + info!("getting file list from: {}", url); let res = client .get(url) .basic_auth(username, Some(password)) @@ -78,6 +82,7 @@ impl Cluster { match res.status() { StatusCode::OK => { let body = res.bytes().await.unwrap(); + info!("got file list len: {}, decompressing", body.len()); let cur = std::io::Cursor::new(body); let raw_data = decode_all(cur); if raw_data.is_err() { @@ -117,12 +122,13 @@ mod tests { test_conf.cluster_port, test_conf.cluster_id, test_conf.cluster_secret, - false, + None, ) } #[tokio::test] async fn test_get_file_list() { + crate::log::init_log_with_cli(); let config = gen_config(); let cluster = Cluster::new(config); cluster.get_file_list().await.unwrap(); diff --git a/src/config.rs b/src/config.rs index a5ddb90..daf07c4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,7 +1,8 @@ +use crate::fatal; + use { - log::{error, warn}, + log::{error, info, warn}, serde::{Deserialize, Serialize}, - serde_json::Result, std::{env, fs}, }; @@ -27,8 +28,8 @@ pub struct Config { impl Config { pub fn new( center_url: Option, - host_ip: Option, - host_port: Option, + host_ip: String, + host_port: u32, cluster_id: String, cluster_secret: String, no_demaon: Option, @@ -36,29 +37,39 @@ impl Config { // https://openbmclapi.bangbang93.com Self { center_url: center_url.unwrap_or("https://openbmclapi.bangbang93.com".to_string()), - host_ip: host_ip.unwrap_or("0.0.0.0".to_string()), - host_port: host_port.unwrap_or(8080), + host_ip, + host_port, cluster_id, cluster_secret, no_demaon: no_demaon.unwrap_or(false), } } - pub fn convert_from_env() { + pub fn new_from_env() { // Load from env - let center_url = env::var("CENTER_URL").ok(); - let host_ip = env::var("CLUSTER_IP").ok(); - let host_port = env::var("CLUSTER_PORT").unwrap().parse::().ok(); + let center_url = env::var("CENTER_URL"); + let center_url = match center_url { + Ok(url) => Some(url), + Err(_) => { + info!("center url not set, use default"); + None + } + }; + let host_ip: String = env::var("CLUSTER_IP").unwrap_or("0.0.0.0".to_string()); + let host_port = env::var("CLUSTER_PORT") + .unwrap_or("8080".to_string()) + .parse::() + .unwrap_or_else(|_| { + fatal!("CLUSTER_PORT must be a number"); + }); let no_demaon = env::var("NO_DAEMON").unwrap().parse::().ok(); // Load from env let cluster_id = env::var("CLUSTER_ID").unwrap_or_else(|err| { - error!("CLUSTER_ID must be set"); - panic!("{}", err); + fatal!("CLUSTER_ID must be set"); }); let cluster_secret = env::var("CLUSTER_SECRET").unwrap_or_else(|err| { - error!("CLUSTER_SECRET must be set"); - panic!("{}", err); + fatal!("CLUSTER_SECRET must be set"); }); // Decrapated warning @@ -103,9 +114,9 @@ impl Config { }); } - pub fn load() -> Result { - todo!("Not implemented yet") - } + // pub fn load() -> Result { + // todo!("Not implemented yet") + // } pub fn join_center_url(&self, path: &str) -> String { format!("{}{}", self.center_url, path) diff --git a/src/log.rs b/src/log.rs index ed79b27..739ebb2 100644 --- a/src/log.rs +++ b/src/log.rs @@ -1 +1,22 @@ -pub fn init_logging() {} +pub fn init_log_with_cli() { + // 命令行参数 + // --warn + // --debug + // --trace + // 从低级开始判断 + let log_level; + if std::env::args().any(|x| x == "--trace") { + log_level = log::LevelFilter::Trace; + } else if std::env::args().any(|x| x == "--debug") { + log_level = log::LevelFilter::Debug; + } else if std::env::args().any(|x| x == "--warn") { + log_level = log::LevelFilter::Warn; + } else { + log_level = log::LevelFilter::Info; + } + simple_logger::SimpleLogger::new() + .with_level(log_level) + .env() + .init() + .unwrap(); +} diff --git a/src/main.rs b/src/main.rs index ac562f2..3c3f315 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,4 +6,6 @@ mod utils; pub const PROTOCOL_VERSION: &str = "1.6.11"; -fn main() {} +fn main() { + log::init_log_with_cli(); +} diff --git a/src/utils.rs b/src/utils.rs index fb5f3f7..6da638c 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,12 +1,12 @@ -use crate::cluster::{SyncFile}; +use crate::cluster::SyncFile; use std::io::Cursor; -use log::{info, warn}; -use std::path::PathBuf; use apache_avro::{from_avro_datum, from_value, types::Value}; +use log::{info, warn}; use md5::{Digest, Md5}; use sha1::Sha1; +use std::path::PathBuf; /// import {join} from 'path' /// @@ -92,6 +92,7 @@ pub fn avro_data_to_file_list(data: Vec) -> Option> { } files.push(try_item.unwrap()); } + info!("parsed {} files", len); Some(files) } _ => { @@ -101,6 +102,16 @@ pub fn avro_data_to_file_list(data: Vec) -> Option> { } } +#[macro_export] +macro_rules! fatal { + ($($arg:tt)+) => { + use log::error; + // error!() + panic!() + error!($($arg)+); + panic!($($arg)+); + }; +} + #[test] fn test_hash_to_filename() { assert_eq!(