From d6dfda136f4eec9c3d9e7108a3774b34e105013a Mon Sep 17 00:00:00 2001 From: shenjack-5600u <3695888@qq.com> Date: Sat, 27 Jan 2024 22:01:36 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E4=B8=8B=20config,=20=E7=8E=B0=E5=9C=A8=E6=9B=B4=E9=98=B3?= =?UTF-8?q?=E9=97=B4=E4=BA=86=E5=91=A2=EF=BC=88=E5=96=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cluster.rs | 20 ++-------- src/config.rs | 99 ++++++++++++-------------------------------------- 2 files changed, 28 insertions(+), 91 deletions(-) diff --git a/src/cluster.rs b/src/cluster.rs index 4ab34d2..1fdfb42 100644 --- a/src/cluster.rs +++ b/src/cluster.rs @@ -1,4 +1,4 @@ -use crate::config::{ClusterByoc, Config}; +use crate::config::{Config}; use crate::utils::avro_data_to_file_list; use crate::PROTOCOL_VERSION; @@ -60,11 +60,7 @@ impl Cluster { pub async fn get_file_list(&self) -> Option> { // server: https://openbmclapi.bangbang93.com // path: /openbmclapi/files - let url = format!( - "{}://{}/openbmclapi/files", - self.config.cluster_byoc.to_string(), - self.config.center_url.clone() - ); + let url = self.config.join_center_url("/openbmclapi/files"); let password = self.config.cluster_secret.clone(); let username = self.config.cluster_id.clone(); 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 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( center_url, + "".to_string(), test_conf.cluster_port, test_conf.cluster_id, test_conf.cluster_secret, - cluster_byoc, - no_demaon, - disable_access_log, - force_noopen, - enable_nginx, + false, ) } diff --git a/src/config.rs b/src/config.rs index 63693ca..3c8d924 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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)] 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, 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 + // pub disable_access_log: bool, + // FORCE_NOOPEN + // pub force_noopen: bool, + // ENABLE_NGINX + // pub enable_nginx: bool, + // NODE_UNIQUE_ID + // pub node_unique_id: String, } 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 { + // https://openbmclapi.bangbang93.com 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") - .unwrap_or("8080".to_string()) - .parse::() - .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::() - .expect("NO_DAEMON must be true or false"); - let disable_access_log = std::env::var("DISABLE_ACCESS_LOG") - .unwrap_or("false".to_string()) - .parse::() - .expect("DISABLE_ACCESS_LOG must be true or false"); - let force_noopen = std::env::var("FORCE_NOOPEN") - .unwrap_or("false".to_string()) - .parse::() - .expect("FORCE_NOOPEN must be true or false"); - let enable_nginx = std::env::var("ENABLE_NGINX") - .unwrap_or("false".to_string()) - .parse::() - .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, - } + + pub fn join_center_url(&self, path: &str) -> String { + format!("{}{}", self.center_url, path) } } From f2efb6b2ff2a2aa2a13a73da3cd58c0f622c1934 Mon Sep 17 00:00:00 2001 From: shenjack-5600u <3695888@qq.com> Date: Sat, 27 Jan 2024 22:14:42 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E8=8B=B1?= =?UTF-8?q?=E8=AF=AD=E8=80=81=E5=B8=88=E6=98=AFGitHub=20copilot=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index 3c8d924..df0941b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,7 +13,7 @@ pub struct Config { pub cluster_id: String, /// CLUSTER_SECRET pub cluster_secret: String, - /// CLUSTER_BYOC + /// NO_DEMAON pub no_demaon: bool, // DISABLE_ACCESS_LOG // pub disable_access_log: bool, From 80d0b05dd726d65dd4f73aabee6f048af980bf2b Mon Sep 17 00:00:00 2001 From: shenjack-5600u <3695888@qq.com> Date: Sat, 27 Jan 2024 22:35:14 +0800 Subject: [PATCH 3/4] rua! --- src/cluster.rs | 3 +-- src/config.rs | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/cluster.rs b/src/cluster.rs index 1fdfb42..a660100 100644 --- a/src/cluster.rs +++ b/src/cluster.rs @@ -107,13 +107,12 @@ mod tests { } fn gen_config() -> Config { - let center_url = "openbmclapi.bangbang93.com".to_string(); // 读取 config.toml 获得 let raw_config = std::fs::read_to_string("config.toml").unwrap(); let test_conf: TestConfig = toml::from_str(raw_config.as_str()).unwrap(); Config::new( - center_url, + None, "".to_string(), test_conf.cluster_port, test_conf.cluster_id, diff --git a/src/config.rs b/src/config.rs index df0941b..7a21d02 100644 --- a/src/config.rs +++ b/src/config.rs @@ -27,7 +27,7 @@ pub struct Config { impl Config { pub fn new( - center_url: String, + center_url: Option, host_ip: String, host_port: u32, cluster_id: String, @@ -36,7 +36,7 @@ impl Config { ) -> Self { // https://openbmclapi.bangbang93.com Self { - center_url, + center_url: center_url.unwrap_or("https://openbmclapi.bangbang93.com".to_string()), host_ip, host_port, cluster_id, From e9ccc8604aee2d8fc3517b27683ca191dbe1319d Mon Sep 17 00:00:00 2001 From: shenjack-5600u <3695888@qq.com> Date: Sat, 27 Jan 2024 22:46:18 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=88=91=E5=BA=94=E8=AF=A5unwrap=E4=B8=80?= =?UTF-8?q?=E4=B8=8B=E7=9A=84=EF=BC=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cluster.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cluster.rs b/src/cluster.rs index a660100..7fab1fa 100644 --- a/src/cluster.rs +++ b/src/cluster.rs @@ -125,6 +125,6 @@ mod tests { async fn test_get_file_list() { let config = gen_config(); let cluster = Cluster::new(config); - cluster.get_file_list().await; + cluster.get_file_list().await.unwrap(); } }