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

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"
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

View File

@ -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<String>,
host_ip: String,
host_port: u32,
host_ip: Option<String>,
host_port: Option<u32>,
cluster_id: String,
cluster_secret: String,
no_demaon: Option<bool>,
) -> 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::<u32>()
.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::<u32>().ok();
let no_demaon = env::var("NO_DAEMON").unwrap().parse::<bool>().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<Self> {
// todo!("Not implemented yet")
// }
pub fn load() -> Result<Self> {
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)