2024-01-27 23:48:49 +08:00
|
|
|
use {
|
|
|
|
log::{error, warn},
|
|
|
|
serde::{Deserialize, Serialize},
|
|
|
|
serde_json::Result,
|
|
|
|
std::{env, fs, io::Error},
|
|
|
|
};
|
2024-01-26 14:32:13 +08:00
|
|
|
|
2024-01-27 23:48:49 +08:00
|
|
|
const CONFIG_PATH: &str = "config.toml";
|
2024-01-27 23:22:54 +08:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
2024-01-26 14:32:13 +08:00
|
|
|
pub struct Config {
|
2024-01-27 23:22:54 +08:00
|
|
|
/// http or https
|
|
|
|
/// CLUSTER_BYOC + CENTER_URL
|
2024-01-26 19:16:59 +08:00
|
|
|
pub center_url: String,
|
2024-01-27 23:22:54 +08:00
|
|
|
/// CLUSTER_IP
|
|
|
|
pub host_ip: String,
|
|
|
|
/// CLUSTER_PORT
|
|
|
|
pub host_port: u32,
|
|
|
|
/// CLUSTER_ID
|
2024-01-26 19:16:59 +08:00
|
|
|
pub cluster_id: String,
|
2024-01-27 23:22:54 +08:00
|
|
|
/// CLUSTER_SECRET
|
2024-01-26 14:32:13 +08:00
|
|
|
pub cluster_secret: String,
|
2024-01-27 23:22:54 +08:00
|
|
|
/// NO_DEMAON
|
2024-01-26 14:32:13 +08:00
|
|
|
pub no_demaon: bool,
|
2024-01-27 23:22:54 +08:00
|
|
|
// DISABLE_ACCESS_LOG [DECRAPEATED]
|
|
|
|
// pub disable_access_log: bool,
|
|
|
|
// FORCE_NOOPEN [DECRAPEATED]
|
|
|
|
// pub force_noopen: bool,
|
|
|
|
// ENABLE_NGINX [DECRAPEATED]
|
|
|
|
// pub enable_nginx: bool,
|
2024-01-26 14:32:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
2024-01-26 19:16:59 +08:00
|
|
|
pub fn new(
|
|
|
|
center_url: String,
|
2024-01-27 23:22:54 +08:00
|
|
|
host_ip: String,
|
|
|
|
host_port: u32,
|
2024-01-26 19:16:59 +08:00
|
|
|
cluster_id: String,
|
|
|
|
cluster_secret: String,
|
|
|
|
no_demaon: bool,
|
|
|
|
) -> Self {
|
2024-01-27 22:01:36 +08:00
|
|
|
// https://openbmclapi.bangbang93.com
|
2024-01-26 19:16:59 +08:00
|
|
|
Self {
|
|
|
|
center_url,
|
2024-01-27 23:22:54 +08:00
|
|
|
host_ip,
|
|
|
|
host_port,
|
2024-01-26 19:16:59 +08:00
|
|
|
cluster_id,
|
|
|
|
cluster_secret,
|
|
|
|
no_demaon,
|
|
|
|
}
|
|
|
|
}
|
2024-01-27 23:22:54 +08:00
|
|
|
|
|
|
|
pub fn convert_from_env() {
|
|
|
|
// Load from env
|
|
|
|
let center_url = env::var("CENTER_URL").unwrap_or("openbmclapi.bangbang93.com".to_string());
|
|
|
|
let host_ip: String = env::var("CLUSTER_IP").unwrap_or("0.0.0.0".to_string());
|
|
|
|
let host_port = env::var("CLUSTER_PORT")
|
2024-01-26 19:16:59 +08:00
|
|
|
.unwrap_or("8080".to_string())
|
2024-01-26 14:32:13 +08:00
|
|
|
.parse::<u32>()
|
2024-01-27 23:22:54 +08:00
|
|
|
.unwrap_or_else(|_| {
|
|
|
|
error!("CLUSTER_PORT must be a number");
|
2024-01-27 23:48:49 +08:00
|
|
|
panic!();
|
2024-01-27 23:22:54 +08:00
|
|
|
});
|
|
|
|
let cluster_id = env::var("CLUSTER_ID").unwrap_or_else(|_| {
|
|
|
|
error!("CLUSTER_ID must be set");
|
2024-01-27 23:48:49 +08:00
|
|
|
panic!();
|
2024-01-27 23:22:54 +08:00
|
|
|
});
|
|
|
|
let cluster_secret = env::var("CLUSTER_SECRET").unwrap_or_else(|_| {
|
|
|
|
error!("CLUSTER_SECRET must be set");
|
2024-01-27 23:48:49 +08:00
|
|
|
panic!();
|
2024-01-27 23:22:54 +08:00
|
|
|
});
|
|
|
|
let no_demaon = env::var("NO_DAEMON")
|
2024-01-26 19:16:59 +08:00
|
|
|
.unwrap_or("false".to_string())
|
2024-01-26 16:50:20 +08:00
|
|
|
.parse::<bool>()
|
2024-01-27 23:22:54 +08:00
|
|
|
.unwrap_or_else(|_| {
|
|
|
|
error!("NO_DAEMON must be true or false");
|
2024-01-27 23:48:49 +08:00
|
|
|
panic!();
|
2024-01-27 23:22:54 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// Decrapated warning
|
|
|
|
if env::var("CLUSTER_BYOC").is_ok() {
|
|
|
|
warn!("CLUSTER_BYOC is deprecated, ignored");
|
|
|
|
}
|
|
|
|
if env::var("DISABLE_ACCESS_LOG").is_ok() {
|
|
|
|
warn!("DISABLE_ACCESS_LOG is deprecated, ignored");
|
2024-01-26 14:32:13 +08:00
|
|
|
}
|
2024-01-27 23:22:54 +08:00
|
|
|
if env::var("FORCE_NOOPEN").is_ok() {
|
|
|
|
warn!("FORCE_NOOPEN is deprecated, ignored");
|
|
|
|
}
|
|
|
|
if env::var("ENABLE_NGINX").is_ok() {
|
|
|
|
warn!("ENABLE_NGINX is deprecated, ignored");
|
|
|
|
// If you want to use Nginx, why would you choose this program?
|
|
|
|
}
|
|
|
|
|
2024-01-27 23:48:49 +08:00
|
|
|
// Create config
|
|
|
|
let config = Config::new(
|
|
|
|
center_url,
|
|
|
|
host_ip,
|
|
|
|
host_port,
|
|
|
|
cluster_id,
|
|
|
|
cluster_secret,
|
|
|
|
no_demaon,
|
|
|
|
);
|
|
|
|
|
2024-01-27 23:22:54 +08:00
|
|
|
// Save config
|
2024-01-27 23:48:49 +08:00
|
|
|
config.save();
|
2024-01-27 23:22:54 +08:00
|
|
|
}
|
2024-01-27 23:48:49 +08:00
|
|
|
pub fn save(&self) {
|
|
|
|
if !fs::canonicalize(CONFIG_PATH).is_ok() {
|
2024-01-27 23:22:54 +08:00
|
|
|
fs::File::create(CONFIG_PATH).unwrap_or_else(|_| {
|
|
|
|
error!("Failed to create config file");
|
2024-01-27 23:48:49 +08:00
|
|
|
panic!();
|
2024-01-27 23:22:54 +08:00
|
|
|
});
|
|
|
|
}
|
2024-01-27 23:48:49 +08:00
|
|
|
fs::write(CONFIG_PATH, toml::to_string(&self).unwrap()).unwrap_or_else(|_| {
|
2024-01-27 23:22:54 +08:00
|
|
|
error!("Failed to save config");
|
2024-01-27 23:48:49 +08:00
|
|
|
panic!();
|
2024-01-27 23:22:54 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load() -> Result<Self> {
|
|
|
|
todo!("Not implemented yet")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn join_center_url(&self, path: &str) -> String {
|
|
|
|
format!("{}{}", self.center_url, path)
|
2024-01-26 14:32:13 +08:00
|
|
|
}
|
|
|
|
}
|