修复了几个小问题,更换配置文件格式

This commit is contained in:
BlueFunny 2024-01-27 23:48:49 +08:00
parent e1e6edd34d
commit 76ce0c4952
No known key found for this signature in database
GPG Key ID: 3F8CF94B07FCBDB3

View File

@ -1,11 +1,11 @@
use log::{debug, error, info, warn}; use {
log::{error, warn},
serde::{Deserialize, Serialize},
serde_json::Result,
std::{env, fs, io::Error},
};
use serde::{Deserialize, Serialize}; const CONFIG_PATH: &str = "config.toml";
use serde_json::Result;
use std::{env, fs, io::Error};
const CONFIG_PATH: &str = "config.json";
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Config { pub struct Config {
@ -59,34 +59,24 @@ impl Config {
.parse::<u32>() .parse::<u32>()
.unwrap_or_else(|_| { .unwrap_or_else(|_| {
error!("CLUSTER_PORT must be a number"); error!("CLUSTER_PORT must be a number");
std::process::exit(1); panic!();
}); });
let cluster_id = env::var("CLUSTER_ID").unwrap_or_else(|_| { let cluster_id = env::var("CLUSTER_ID").unwrap_or_else(|_| {
error!("CLUSTER_ID must be set"); error!("CLUSTER_ID must be set");
std::process::exit(1); panic!();
}); });
let cluster_secret = env::var("CLUSTER_SECRET").unwrap_or_else(|_| { let cluster_secret = env::var("CLUSTER_SECRET").unwrap_or_else(|_| {
error!("CLUSTER_SECRET must be set"); error!("CLUSTER_SECRET must be set");
std::process::exit(1); panic!();
}); });
let no_demaon = env::var("NO_DAEMON") let no_demaon = env::var("NO_DAEMON")
.unwrap_or("false".to_string()) .unwrap_or("false".to_string())
.parse::<bool>() .parse::<bool>()
.unwrap_or_else(|_| { .unwrap_or_else(|_| {
error!("NO_DAEMON must be true or false"); error!("NO_DAEMON must be true or false");
std::process::exit(1); panic!();
}); });
// Create config
let config = Config::new(
center_url,
host_ip,
host_port,
cluster_id,
cluster_secret,
no_demaon,
);
// 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");
@ -102,19 +92,29 @@ impl Config {
// If you want to use Nginx, why would you choose this program? // If you want to use Nginx, why would you choose this program?
} }
// Create config
let config = Config::new(
center_url,
host_ip,
host_port,
cluster_id,
cluster_secret,
no_demaon,
);
// Save config // Save config
Self::save(config); config.save();
} }
pub fn save(config: Config) { 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(|_| { fs::File::create(CONFIG_PATH).unwrap_or_else(|_| {
error!("Failed to create config file"); error!("Failed to create config file");
std::process::exit(1); panic!();
}); });
} }
fs::write(CONFIG_PATH, serde_json::to_string(&config).unwrap()).unwrap_or_else(|_| { fs::write(CONFIG_PATH, toml::to_string(&self).unwrap()).unwrap_or_else(|_| {
error!("Failed to save config"); error!("Failed to save config");
std::process::exit(1); panic!();
}); });
} }