diff --git a/Cargo.toml b/Cargo.toml index a4bd084..a2f4a5a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,9 @@ sha1 = "0.10.6" zstd = "0.13.0" log = "0.4.20" -simple_logger = "4.3.3" +[dependencies.simple_logger] +version = "4.3.3" +features = ["colors", "threads", "timestamps", "stderr"] # [dependencies.db_logger] # version = "0.1" # optional = true diff --git a/src/config.rs b/src/config.rs index daf07c4..79a75ab 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,8 +1,6 @@ -use crate::fatal; - use { + crate::fatal, log::{error, info, warn}, - serde::{Deserialize, Serialize}, std::{env, fs}, }; @@ -28,40 +26,27 @@ pub struct Config { impl Config { pub fn new( center_url: Option, - host_ip: String, - host_port: u32, + host_ip: Option, + host_port: Option, cluster_id: String, cluster_secret: String, no_demaon: Option, ) -> Self { - // https://openbmclapi.bangbang93.com Self { center_url: center_url.unwrap_or("https://openbmclapi.bangbang93.com".to_string()), - host_ip, - host_port, + host_ip: host_ip.unwrap_or("0.0.0.0".to_string()), + host_port: host_port.unwrap_or(8080), cluster_id, cluster_secret, no_demaon: no_demaon.unwrap_or(false), } } - pub fn new_from_env() { + pub fn convert_from_env() { // Load from env - 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 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 no_demaon = env::var("NO_DAEMON").unwrap().parse::().ok(); // Load from env @@ -71,7 +56,7 @@ impl Config { let cluster_secret = env::var("CLUSTER_SECRET").unwrap_or_else(|err| { fatal!("CLUSTER_SECRET must be set"); }); - + // Decrapated warning if env::var("CLUSTER_BYOC").is_ok() { warn!("CLUSTER_BYOC is deprecated, ignored"); @@ -103,20 +88,20 @@ impl Config { pub fn save(&self) { if !fs::canonicalize(CONFIG_PATH).is_ok() { fs::File::create(CONFIG_PATH).unwrap_or_else(|err| { - error!("Failed to create config file"); - panic!("{}", err); + fatal!("Failed to create config file"); }); //TODO: Trigger initialization } fs::write(CONFIG_PATH, toml::to_string(&self).unwrap()).unwrap_or_else(|err| { - error!("Failed to save config"); - panic!("{}", err); + fatal!("Failed to save config file"); }); } - // pub fn load() -> Result { - // todo!("Not implemented yet") - // } + pub fn load() -> Result { + toml::from_str(&self.load_raw()?).map_err(|err| { + fatal!("Failed to parse config file"); + }) + } pub fn join_center_url(&self, path: &str) -> String { format!("{}{}", self.center_url, path)