修改了一下 config, 现在更阳间了呢(喜

This commit is contained in:
shenjack-5600u 2024-01-27 22:01:36 +08:00
parent badc29ae9f
commit d6dfda136f
2 changed files with 28 additions and 91 deletions

View File

@ -1,4 +1,4 @@
use crate::config::{ClusterByoc, Config}; use crate::config::{Config};
use crate::utils::avro_data_to_file_list; use crate::utils::avro_data_to_file_list;
use crate::PROTOCOL_VERSION; use crate::PROTOCOL_VERSION;
@ -60,11 +60,7 @@ impl Cluster {
pub async fn get_file_list(&self) -> Option<Vec<SyncFile>> { pub async fn get_file_list(&self) -> Option<Vec<SyncFile>> {
// server: https://openbmclapi.bangbang93.com // server: https://openbmclapi.bangbang93.com
// path: /openbmclapi/files // path: /openbmclapi/files
let url = format!( let url = self.config.join_center_url("/openbmclapi/files");
"{}://{}/openbmclapi/files",
self.config.cluster_byoc.to_string(),
self.config.center_url.clone()
);
let password = self.config.cluster_secret.clone(); let password = self.config.cluster_secret.clone();
let username = self.config.cluster_id.clone(); let username = self.config.cluster_id.clone();
let client = Client::builder().user_agent(self.ua.clone()).build().unwrap(); let client = Client::builder().user_agent(self.ua.clone()).build().unwrap();
@ -116,21 +112,13 @@ mod tests {
let raw_config = std::fs::read_to_string("config.toml").unwrap(); let raw_config = std::fs::read_to_string("config.toml").unwrap();
let test_conf: TestConfig = toml::from_str(raw_config.as_str()).unwrap(); let test_conf: TestConfig = toml::from_str(raw_config.as_str()).unwrap();
let cluster_byoc = ClusterByoc::https;
let no_demaon = false;
let disable_access_log = false;
let force_noopen = false;
let enable_nginx = false;
Config::new( Config::new(
center_url, center_url,
"".to_string(),
test_conf.cluster_port, test_conf.cluster_port,
test_conf.cluster_id, test_conf.cluster_id,
test_conf.cluster_secret, test_conf.cluster_secret,
cluster_byoc, false,
no_demaon,
disable_access_log,
force_noopen,
enable_nginx,
) )
} }

View File

@ -1,102 +1,51 @@
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ClusterByoc {
http,
https,
}
impl ClusterByoc {
pub fn to_string(&self) -> String {
match self {
ClusterByoc::http => "http".to_string(),
ClusterByoc::https => "https".to_string(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Config { pub struct Config {
/// http or https
/// CLUSTER_BYOC + CENTER_URL
pub center_url: String, 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, pub cluster_id: String,
/// CLUSTER_SECRET
pub cluster_secret: String, pub cluster_secret: String,
/// CLUSTER_BYOC /// CLUSTER_BYOC
pub cluster_byoc: ClusterByoc,
pub no_demaon: bool, pub no_demaon: bool,
/// DISABLE_ACCESS_LOG // DISABLE_ACCESS_LOG
pub disable_access_log: bool, // pub disable_access_log: bool,
/// FORCE_NOOPEN // FORCE_NOOPEN
pub force_noopen: bool, // pub force_noopen: bool,
/// ENABLE_NGINX // ENABLE_NGINX
pub enable_nginx: bool, // pub enable_nginx: bool,
// NODE_UNIQUE_ID
// pub node_unique_id: String,
} }
impl Config { impl Config {
pub fn new( pub fn new(
center_url: String, center_url: String,
cluster_port: u32, host_ip: String,
host_port: u32,
cluster_id: String, cluster_id: String,
cluster_secret: String, cluster_secret: String,
cluster_byoc: ClusterByoc,
no_demaon: bool, no_demaon: bool,
disable_access_log: bool,
force_noopen: bool,
enable_nginx: bool,
) -> Self { ) -> Self {
// https://openbmclapi.bangbang93.com
Self { Self {
center_url, center_url,
cluster_port, host_ip,
host_port,
cluster_id, cluster_id,
cluster_secret, cluster_secret,
cluster_byoc,
no_demaon, no_demaon,
disable_access_log,
force_noopen,
enable_nginx,
} }
} }
pub fn new_from_env() -> Self {
let center_url = pub fn join_center_url(&self, path: &str) -> String {
std::env::var("CENTER_URL").unwrap_or("openbmclapi.bangbang93.com".to_string()); format!("{}{}", self.center_url, path)
let cluster_port = std::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("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 {
center_url,
cluster_port,
cluster_id,
cluster_secret,
cluster_byoc,
no_demaon,
disable_access_log,
force_noopen,
enable_nginx,
}
} }
} }