2024-01-28 00:20:05 +08:00
|
|
|
use crate::fatal;
|
|
|
|
|
2024-01-27 23:48:49 +08:00
|
|
|
use {
|
2024-01-28 00:20:05 +08:00
|
|
|
log::{error, info, warn},
|
2024-01-27 23:48:49 +08:00
|
|
|
serde::{Deserialize, Serialize},
|
2024-01-28 00:20:02 +08:00
|
|
|
std::{env, fs},
|
2024-01-27 23:48:49 +08:00
|
|
|
};
|
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
|
2024-01-28 00:20:02 +08:00
|
|
|
/// 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,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
2024-01-26 19:16:59 +08:00
|
|
|
pub fn new(
|
2024-01-28 00:20:02 +08:00
|
|
|
center_url: Option<String>,
|
2024-01-28 00:20:05 +08:00
|
|
|
host_ip: String,
|
|
|
|
host_port: u32,
|
2024-01-26 19:16:59 +08:00
|
|
|
cluster_id: String,
|
|
|
|
cluster_secret: String,
|
2024-01-28 00:20:02 +08:00
|
|
|
no_demaon: Option<bool>,
|
2024-01-26 19:16:59 +08:00
|
|
|
) -> Self {
|
2024-01-27 22:01:36 +08:00
|
|
|
// https://openbmclapi.bangbang93.com
|
2024-01-26 19:16:59 +08:00
|
|
|
Self {
|
2024-01-28 00:20:02 +08:00
|
|
|
center_url: center_url.unwrap_or("https://openbmclapi.bangbang93.com".to_string()),
|
2024-01-28 00:20:05 +08:00
|
|
|
host_ip,
|
|
|
|
host_port,
|
2024-01-26 19:16:59 +08:00
|
|
|
cluster_id,
|
|
|
|
cluster_secret,
|
2024-01-28 00:20:02 +08:00
|
|
|
no_demaon: no_demaon.unwrap_or(false),
|
2024-01-26 19:16:59 +08:00
|
|
|
}
|
|
|
|
}
|
2024-01-27 23:22:54 +08:00
|
|
|
|
2024-01-28 00:20:05 +08:00
|
|
|
pub fn new_from_env() {
|
2024-01-27 23:22:54 +08:00
|
|
|
// Load from env
|
2024-01-28 00:20:05 +08:00
|
|
|
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");
|
|
|
|
});
|
2024-01-28 00:20:02 +08:00
|
|
|
let no_demaon = env::var("NO_DAEMON").unwrap().parse::<bool>().ok();
|
|
|
|
|
|
|
|
// Load from env
|
|
|
|
let cluster_id = env::var("CLUSTER_ID").unwrap_or_else(|err| {
|
2024-01-28 00:20:05 +08:00
|
|
|
fatal!("CLUSTER_ID must be set");
|
2024-01-27 23:22:54 +08:00
|
|
|
});
|
2024-01-28 00:20:02 +08:00
|
|
|
let cluster_secret = env::var("CLUSTER_SECRET").unwrap_or_else(|err| {
|
2024-01-28 00:20:05 +08:00
|
|
|
fatal!("CLUSTER_SECRET must be set");
|
2024-01-27 23:22:54 +08:00
|
|
|
});
|
2024-01-28 00:20:02 +08:00
|
|
|
|
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-28 00:20:02 +08:00
|
|
|
fs::File::create(CONFIG_PATH).unwrap_or_else(|err| {
|
2024-01-27 23:22:54 +08:00
|
|
|
error!("Failed to create config file");
|
2024-01-28 00:20:02 +08:00
|
|
|
panic!("{}", err);
|
2024-01-27 23:22:54 +08:00
|
|
|
});
|
2024-01-28 00:20:02 +08:00
|
|
|
//TODO: Trigger initialization
|
2024-01-27 23:22:54 +08:00
|
|
|
}
|
2024-01-28 00:20:02 +08:00
|
|
|
fs::write(CONFIG_PATH, toml::to_string(&self).unwrap()).unwrap_or_else(|err| {
|
2024-01-27 23:22:54 +08:00
|
|
|
error!("Failed to save config");
|
2024-01-28 00:20:02 +08:00
|
|
|
panic!("{}", err);
|
2024-01-27 23:22:54 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-01-28 00:20:05 +08:00
|
|
|
// pub fn load() -> Result<Self> {
|
|
|
|
// todo!("Not implemented yet")
|
|
|
|
// }
|
2024-01-27 23:22:54 +08:00
|
|
|
|
|
|
|
pub fn join_center_url(&self, path: &str) -> String {
|
|
|
|
format!("{}{}", self.center_url, path)
|
2024-01-26 14:32:13 +08:00
|
|
|
}
|
|
|
|
}
|