也许完成了解析部分 (未经验证)

This commit is contained in:
BlueFunny 2024-01-28 00:51:45 +08:00
parent 837b6bbbbd
commit 312bb60f72
No known key found for this signature in database
GPG Key ID: 3F8CF94B07FCBDB3
2 changed files with 20 additions and 33 deletions

View File

@ -27,7 +27,9 @@ sha1 = "0.10.6"
zstd = "0.13.0" zstd = "0.13.0"
log = "0.4.20" log = "0.4.20"
simple_logger = "4.3.3" [dependencies.simple_logger]
version = "4.3.3"
features = ["colors", "threads", "timestamps", "stderr"]
# [dependencies.db_logger] # [dependencies.db_logger]
# version = "0.1" # version = "0.1"
# optional = true # optional = true

View File

@ -1,8 +1,6 @@
use crate::fatal;
use { use {
crate::fatal,
log::{error, info, warn}, log::{error, info, warn},
serde::{Deserialize, Serialize},
std::{env, fs}, std::{env, fs},
}; };
@ -28,40 +26,27 @@ pub struct Config {
impl Config { impl Config {
pub fn new( pub fn new(
center_url: Option<String>, center_url: Option<String>,
host_ip: String, host_ip: Option<String>,
host_port: u32, host_port: Option<u32>,
cluster_id: String, cluster_id: String,
cluster_secret: String, cluster_secret: String,
no_demaon: Option<bool>, no_demaon: Option<bool>,
) -> Self { ) -> Self {
// https://openbmclapi.bangbang93.com
Self { Self {
center_url: center_url.unwrap_or("https://openbmclapi.bangbang93.com".to_string()), center_url: center_url.unwrap_or("https://openbmclapi.bangbang93.com".to_string()),
host_ip, host_ip: host_ip.unwrap_or("0.0.0.0".to_string()),
host_port, host_port: host_port.unwrap_or(8080),
cluster_id, cluster_id,
cluster_secret, cluster_secret,
no_demaon: no_demaon.unwrap_or(false), no_demaon: no_demaon.unwrap_or(false),
} }
} }
pub fn new_from_env() { pub fn convert_from_env() {
// Load from env // Load from env
let center_url = env::var("CENTER_URL"); let center_url = env::var("CENTER_URL").ok();
let center_url = match center_url { let host_ip = env::var("CLUSTER_IP").ok();
Ok(url) => Some(url), let host_port = env::var("CLUSTER_PORT").unwrap().parse::<u32>().ok();
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::<u32>()
.unwrap_or_else(|_| {
fatal!("CLUSTER_PORT must be a number");
});
let no_demaon = env::var("NO_DAEMON").unwrap().parse::<bool>().ok(); let no_demaon = env::var("NO_DAEMON").unwrap().parse::<bool>().ok();
// Load from env // Load from env
@ -71,7 +56,7 @@ impl Config {
let cluster_secret = env::var("CLUSTER_SECRET").unwrap_or_else(|err| { let cluster_secret = env::var("CLUSTER_SECRET").unwrap_or_else(|err| {
fatal!("CLUSTER_SECRET must be set"); fatal!("CLUSTER_SECRET must be set");
}); });
// Decrapated warning // Decrapated warning
if env::var("CLUSTER_BYOC").is_ok() { if env::var("CLUSTER_BYOC").is_ok() {
warn!("CLUSTER_BYOC is deprecated, ignored"); warn!("CLUSTER_BYOC is deprecated, ignored");
@ -103,20 +88,20 @@ impl Config {
pub fn save(&self) { pub fn save(&self) {
if !fs::canonicalize(CONFIG_PATH).is_ok() { if !fs::canonicalize(CONFIG_PATH).is_ok() {
fs::File::create(CONFIG_PATH).unwrap_or_else(|err| { fs::File::create(CONFIG_PATH).unwrap_or_else(|err| {
error!("Failed to create config file"); fatal!("Failed to create config file");
panic!("{}", err);
}); });
//TODO: Trigger initialization //TODO: Trigger initialization
} }
fs::write(CONFIG_PATH, toml::to_string(&self).unwrap()).unwrap_or_else(|err| { fs::write(CONFIG_PATH, toml::to_string(&self).unwrap()).unwrap_or_else(|err| {
error!("Failed to save config"); fatal!("Failed to save config file");
panic!("{}", err);
}); });
} }
// pub fn load() -> Result<Self> { pub fn load() -> Result<Self> {
// todo!("Not implemented yet") toml::from_str(&self.load_raw()?).map_err(|err| {
// } fatal!("Failed to parse config file");
})
}
pub fn join_center_url(&self, path: &str) -> String { pub fn join_center_url(&self, path: &str) -> String {
format!("{}{}", self.center_url, path) format!("{}{}", self.center_url, path)