提供了默认配置, 小修了几个地方
This commit is contained in:
parent
76ce0c4952
commit
c98acf824d
@ -2,7 +2,7 @@ use {
|
|||||||
log::{error, warn},
|
log::{error, warn},
|
||||||
serde::{Deserialize, Serialize},
|
serde::{Deserialize, Serialize},
|
||||||
serde_json::Result,
|
serde_json::Result,
|
||||||
std::{env, fs, io::Error},
|
std::{env, fs},
|
||||||
};
|
};
|
||||||
|
|
||||||
const CONFIG_PATH: &str = "config.toml";
|
const CONFIG_PATH: &str = "config.toml";
|
||||||
@ -10,7 +10,7 @@ const CONFIG_PATH: &str = "config.toml";
|
|||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
/// http or https
|
/// http or https
|
||||||
/// CLUSTER_BYOC + CENTER_URL
|
/// CENTER_URL
|
||||||
pub center_url: String,
|
pub center_url: String,
|
||||||
/// CLUSTER_IP
|
/// CLUSTER_IP
|
||||||
pub host_ip: String,
|
pub host_ip: String,
|
||||||
@ -22,61 +22,45 @@ pub struct Config {
|
|||||||
pub cluster_secret: String,
|
pub cluster_secret: String,
|
||||||
/// NO_DEMAON
|
/// NO_DEMAON
|
||||||
pub no_demaon: bool,
|
pub no_demaon: bool,
|
||||||
// DISABLE_ACCESS_LOG [DECRAPEATED]
|
|
||||||
// pub disable_access_log: bool,
|
|
||||||
// FORCE_NOOPEN [DECRAPEATED]
|
|
||||||
// pub force_noopen: bool,
|
|
||||||
// ENABLE_NGINX [DECRAPEATED]
|
|
||||||
// pub enable_nginx: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
center_url: 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: bool,
|
no_demaon: Option<bool>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
// https://openbmclapi.bangbang93.com
|
// https://openbmclapi.bangbang93.com
|
||||||
Self {
|
Self {
|
||||||
center_url,
|
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: no_demaon.unwrap_or(false),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn convert_from_env() {
|
pub fn convert_from_env() {
|
||||||
// Load from env
|
// Load from env
|
||||||
let center_url = env::var("CENTER_URL").unwrap_or("openbmclapi.bangbang93.com".to_string());
|
let center_url = env::var("CENTER_URL").ok();
|
||||||
let host_ip: String = env::var("CLUSTER_IP").unwrap_or("0.0.0.0".to_string());
|
let host_ip = env::var("CLUSTER_IP").ok();
|
||||||
let host_port = env::var("CLUSTER_PORT")
|
let host_port = env::var("CLUSTER_PORT").unwrap().parse::<u32>().ok();
|
||||||
.unwrap_or("8080".to_string())
|
let no_demaon = env::var("NO_DAEMON").unwrap().parse::<bool>().ok();
|
||||||
.parse::<u32>()
|
|
||||||
.unwrap_or_else(|_| {
|
|
||||||
error!("CLUSTER_PORT must be a number");
|
|
||||||
panic!();
|
|
||||||
});
|
|
||||||
let cluster_id = env::var("CLUSTER_ID").unwrap_or_else(|_| {
|
|
||||||
error!("CLUSTER_ID must be set");
|
|
||||||
panic!();
|
|
||||||
});
|
|
||||||
let cluster_secret = env::var("CLUSTER_SECRET").unwrap_or_else(|_| {
|
|
||||||
error!("CLUSTER_SECRET must be set");
|
|
||||||
panic!();
|
|
||||||
});
|
|
||||||
let no_demaon = env::var("NO_DAEMON")
|
|
||||||
.unwrap_or("false".to_string())
|
|
||||||
.parse::<bool>()
|
|
||||||
.unwrap_or_else(|_| {
|
|
||||||
error!("NO_DAEMON must be true or false");
|
|
||||||
panic!();
|
|
||||||
});
|
|
||||||
|
|
||||||
|
// Load from env
|
||||||
|
let cluster_id = env::var("CLUSTER_ID").unwrap_or_else(|err| {
|
||||||
|
error!("CLUSTER_ID must be set");
|
||||||
|
panic!("{}", err);
|
||||||
|
});
|
||||||
|
let cluster_secret = env::var("CLUSTER_SECRET").unwrap_or_else(|err| {
|
||||||
|
error!("CLUSTER_SECRET must be set");
|
||||||
|
panic!("{}", err);
|
||||||
|
});
|
||||||
|
|
||||||
// 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");
|
||||||
@ -107,14 +91,15 @@ 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(|_| {
|
fs::File::create(CONFIG_PATH).unwrap_or_else(|err| {
|
||||||
error!("Failed to create config file");
|
error!("Failed to create config file");
|
||||||
panic!();
|
panic!("{}", err);
|
||||||
});
|
});
|
||||||
|
//TODO: Trigger initialization
|
||||||
}
|
}
|
||||||
fs::write(CONFIG_PATH, toml::to_string(&self).unwrap()).unwrap_or_else(|_| {
|
fs::write(CONFIG_PATH, toml::to_string(&self).unwrap()).unwrap_or_else(|err| {
|
||||||
error!("Failed to save config");
|
error!("Failed to save config");
|
||||||
panic!();
|
panic!("{}", err);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user