完成了 Config 的转换与保存部分
This commit is contained in:
parent
badc29ae9f
commit
b6275290d3
@ -17,7 +17,7 @@ axum = "0.7.4"
|
||||
tokio = { version = "1.35.1", features = ["full"] }
|
||||
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
serde_json = "1.0.112"
|
||||
toml = "0.8.8"
|
||||
apache-avro = "0.16.0"
|
||||
|
||||
|
161
src/config.rs
161
src/config.rs
@ -1,102 +1,127 @@
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum ClusterByoc {
|
||||
http,
|
||||
https,
|
||||
}
|
||||
use log::{debug, error, info, warn};
|
||||
|
||||
impl ClusterByoc {
|
||||
pub fn to_string(&self) -> String {
|
||||
match self {
|
||||
ClusterByoc::http => "http".to_string(),
|
||||
ClusterByoc::https => "https".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Result;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
use std::{env, fs, io::Error};
|
||||
|
||||
const CONFIG_PATH: &str = "config.json";
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
pub struct Config {
|
||||
/// http or https
|
||||
/// CLUSTER_BYOC + CENTER_URL
|
||||
pub center_url: String,
|
||||
pub cluster_port: u32,
|
||||
/// CLUSTER_IP
|
||||
pub host_ip: String,
|
||||
/// CLUSTER_PORT
|
||||
pub host_port: u32,
|
||||
/// CLUSTER_ID
|
||||
pub cluster_id: String,
|
||||
/// CLUSTER_SECRET
|
||||
pub cluster_secret: String,
|
||||
/// CLUSTER_BYOC
|
||||
pub cluster_byoc: ClusterByoc,
|
||||
/// NO_DEMAON
|
||||
pub no_demaon: bool,
|
||||
/// DISABLE_ACCESS_LOG
|
||||
pub disable_access_log: bool,
|
||||
/// FORCE_NOOPEN
|
||||
pub force_noopen: bool,
|
||||
/// ENABLE_NGINX
|
||||
pub enable_nginx: 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 {
|
||||
pub fn new(
|
||||
center_url: String,
|
||||
cluster_port: u32,
|
||||
host_ip: String,
|
||||
host_port: u32,
|
||||
cluster_id: String,
|
||||
cluster_secret: String,
|
||||
cluster_byoc: ClusterByoc,
|
||||
no_demaon: bool,
|
||||
disable_access_log: bool,
|
||||
force_noopen: bool,
|
||||
enable_nginx: bool,
|
||||
) -> Self {
|
||||
Self {
|
||||
center_url,
|
||||
cluster_port,
|
||||
host_ip,
|
||||
host_port,
|
||||
cluster_id,
|
||||
cluster_secret,
|
||||
cluster_byoc,
|
||||
no_demaon,
|
||||
disable_access_log,
|
||||
force_noopen,
|
||||
enable_nginx,
|
||||
}
|
||||
}
|
||||
pub fn new_from_env() -> Self {
|
||||
let center_url =
|
||||
std::env::var("CENTER_URL").unwrap_or("openbmclapi.bangbang93.com".to_string());
|
||||
let cluster_port = std::env::var("CLUSTER_PORT")
|
||||
|
||||
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")
|
||||
.unwrap_or("8080".to_string())
|
||||
.parse::<u32>()
|
||||
.expect("CLUSTER_PORT must be a number");
|
||||
let cluster_id = std::env::var("CLUSTER_ID").unwrap_or("1".to_string());
|
||||
let cluster_secret = std::env::var("CLUSTER_SECRET").expect("CLUSTER_SECRET must be set");
|
||||
let cluster_byoc = match std::env::var("CLUSTER_BYOC")
|
||||
.unwrap_or("http".to_string())
|
||||
.as_str()
|
||||
{
|
||||
"http" => ClusterByoc::http,
|
||||
"https" => ClusterByoc::https,
|
||||
_ => panic!("CLUSTER_BYOC must be http or https"),
|
||||
};
|
||||
let no_demaon = std::env::var("NO_DAEMON")
|
||||
.unwrap_or_else(|_| {
|
||||
error!("CLUSTER_PORT must be a number");
|
||||
std::process::exit(1);
|
||||
});
|
||||
let cluster_id = env::var("CLUSTER_ID").unwrap_or_else(|_| {
|
||||
error!("CLUSTER_ID must be set");
|
||||
std::process::exit(1);
|
||||
});
|
||||
let cluster_secret = env::var("CLUSTER_SECRET").unwrap_or_else(|_| {
|
||||
error!("CLUSTER_SECRET must be set");
|
||||
std::process::exit(1);
|
||||
});
|
||||
let no_demaon = env::var("NO_DAEMON")
|
||||
.unwrap_or("false".to_string())
|
||||
.parse::<bool>()
|
||||
.expect("NO_DAEMON must be true or false");
|
||||
let disable_access_log = std::env::var("DISABLE_ACCESS_LOG")
|
||||
.unwrap_or("false".to_string())
|
||||
.parse::<bool>()
|
||||
.expect("DISABLE_ACCESS_LOG must be true or false");
|
||||
let force_noopen = std::env::var("FORCE_NOOPEN")
|
||||
.unwrap_or("false".to_string())
|
||||
.parse::<bool>()
|
||||
.expect("FORCE_NOOPEN must be true or false");
|
||||
let enable_nginx = std::env::var("ENABLE_NGINX")
|
||||
.unwrap_or("false".to_string())
|
||||
.parse::<bool>()
|
||||
.expect("ENABLE_NGINX must be true or false");
|
||||
Self {
|
||||
.unwrap_or_else(|_| {
|
||||
error!("NO_DAEMON must be true or false");
|
||||
std::process::exit(1);
|
||||
});
|
||||
|
||||
// Create config
|
||||
let config = Config::new(
|
||||
center_url,
|
||||
cluster_port,
|
||||
host_ip,
|
||||
host_port,
|
||||
cluster_id,
|
||||
cluster_secret,
|
||||
cluster_byoc,
|
||||
no_demaon,
|
||||
disable_access_log,
|
||||
force_noopen,
|
||||
enable_nginx,
|
||||
);
|
||||
|
||||
// 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");
|
||||
}
|
||||
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?
|
||||
}
|
||||
|
||||
// Save config
|
||||
Self::save(config);
|
||||
}
|
||||
pub fn save(config: Config) {
|
||||
if ! fs::canonicalize(CONFIG_PATH).is_ok() {
|
||||
fs::File::create(CONFIG_PATH).unwrap_or_else(|_| {
|
||||
error!("Failed to create config file");
|
||||
std::process::exit(1);
|
||||
});
|
||||
}
|
||||
fs::write(CONFIG_PATH, serde_json::to_string(&config).unwrap()).unwrap_or_else(|_| {
|
||||
error!("Failed to save config");
|
||||
std::process::exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
pub fn load() -> Result<Self> {
|
||||
todo!("Not implemented yet")
|
||||
}
|
||||
|
||||
pub fn join_center_url(&self, path: &str) -> String {
|
||||
format!("{}{}", self.center_url, path)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user