From d851dbbea6ae3d46ccebfba56cdeeee5f37908dd Mon Sep 17 00:00:00 2001 From: naskya Date: Sun, 23 Jun 2024 16:57:36 +0900 Subject: [PATCH] WIP --- src/command/config/update/v1.rs | 115 ++++++++++++++++++++++---------- src/config/server.rs | 56 +++++++++++++--- 2 files changed, 125 insertions(+), 46 deletions(-) diff --git a/src/command/config/update/v1.rs b/src/command/config/update/v1.rs index 3872913..bd5e2ec 100644 --- a/src/command/config/update/v1.rs +++ b/src/command/config/update/v1.rs @@ -291,13 +291,10 @@ fn create_new_server_config( )); } - let hostname = parsed_server_url.host_str().ok_or(Error::InvalidConfig( - "hostname is missing in the server url", - ))?; - let host = match parsed_server_url.port() { - Some(port) => format!("{}:{}", hostname, port), - None => hostname.to_owned(), - }; + let domain = parsed_server_url + .host_str() + .ok_or(Error::InvalidConfig("domain is missing in the server url"))? + .to_string(); let repository_url = match meta.repository_url.as_ref() { "https://codeberg.org/calckey/calckey" @@ -307,6 +304,63 @@ fn create_new_server_config( url => Some(url.to_owned()), }; + let id: Option = if let Some(id) = default_yml.get("cuid") { + let id = id.as_hash().ok_or(Error::InvalidConfig("cuid"))?; + Some(server::Id { + length: match id.get(&Yaml::String("length".to_string())) { + Some(length) => { + Some(length.as_i64().ok_or(Error::InvalidConfig("cuid.length"))? as u8) + } + None => None, + }, + fingerprint: match id.get(&Yaml::String("fingerprint".to_string())) { + Some(fingerprint) => Some( + fingerprint + .as_str() + .ok_or(Error::InvalidConfig("cuid.fingerprint"))? + .to_string(), + ), + None => None, + }, + }) + } else { + None + }; + + let file = { + let mut file = server::File { + max_size: None, + proxy_remote_file: None, + cache_remote_file: None, + }; + + if let Some(max_size) = default_yml.get("maxFileSize") { + let max_size_mb = max_size + .as_i64() + .ok_or(Error::InvalidConfig("maxFileSize is not an integer"))? + / 1_000_000; + file.max_size = Some(max_size_mb as u64); + } + if let Some(proxy_remote_files) = default_yml.get("proxyRemoteFiles") { + let proxy_remote_files = proxy_remote_files + .as_bool() + .ok_or(Error::InvalidConfig("proxyRemoteFiles is not bool"))?; + file.proxy_remote_file = Some(proxy_remote_files); + } + if meta.cache_remote_files { + file.cache_remote_file = Some(true); + } + + if file.max_size.is_none() + && file.proxy_remote_file.is_none() + && file.proxy_remote_file.is_none() + { + None + } else { + Some(file) + } + }; + let mut server_config = server::Config { config_revision: Revision::V1, info: Some(server::Info { @@ -317,7 +371,7 @@ fn create_new_server_config( open_registrations: !meta.disable_registration, repository_url, }), - timelines: Some(server::Timelines { + timeline: Some(server::Timeline { local: !meta.disable_local_timeline, global: !meta.disable_global_timeline, recommended: !meta.disable_recommended_timeline, @@ -328,11 +382,22 @@ fn create_new_server_config( "http" => Some(server::HttpProtocol::Http), _ => None, }, - host, - port: default_yml - .get("port") - .and_then(|v| v.as_i64()) - .ok_or(Error::InvalidConfig("port"))? as u16, + domain, + port: parsed_server_url.port(), + listen: server::Listen { + host: default_yml + .get("bind") + .and_then(|v| v.as_str()) + .map(|v| v.to_string()), + port: default_yml + .get("port") + .and_then(|v| v.as_i64()) + .ok_or(Error::InvalidConfig("port"))? as u16, + }, + http_proxy: todo!(), + media_proxy: todo!(), + summaly_proxy: todo!(), + smtp_proxy: todo!(), }, database: server::Database { host: db @@ -400,30 +465,10 @@ fn create_new_server_config( None => None, }, }, - id: None, + id, + file, }; - if let Some(id) = default_yml.get("cuid") { - let id = id.as_hash().ok_or(Error::InvalidConfig("cuid"))?; - server_config.id = Some(server::Id { - length: match id.get(&Yaml::String("length".to_string())) { - Some(length) => { - Some(length.as_i64().ok_or(Error::InvalidConfig("cuid.length"))? as u8) - } - None => None, - }, - fingerprint: match id.get(&Yaml::String("fingerprint".to_string())) { - Some(fingerprint) => Some( - fingerprint - .as_str() - .ok_or(Error::InvalidConfig("cuid.fingerprint"))? - .to_string(), - ), - None => None, - }, - }); - } - Ok(server_config) } diff --git a/src/config/server.rs b/src/config/server.rs index 39820ac..7846183 100644 --- a/src/config/server.rs +++ b/src/config/server.rs @@ -14,17 +14,50 @@ pub struct Config { #[validate(custom(function = "ensure_latest_revision"))] pub config_revision: Revision, #[validate(nested)] + pub network: Network, + #[validate(nested)] pub info: Option, #[validate(nested)] - pub timelines: Option, - #[validate(nested)] - pub network: Network, + pub timeline: Option, #[validate(nested)] pub database: Database, #[validate(nested)] pub cache_server: CacheServer, #[validate(nested)] pub id: Option, + #[validate(nested)] + pub file: Option, +} + +#[derive(Deserialize, Serialize, Validate, Debug)] +pub struct Network { + pub protocol: Option, + pub domain: String, + pub port: Option, + #[validate(nested)] + pub listen: Listen, + #[validate(nested)] + pub http_proxy: Option, + #[validate(nested)] + pub media_proxy: Option, + #[validate(nested)] + pub summaly_proxy: Option, + #[validate(nested)] + pub smtp_proxy: Option, +} + +#[derive(Deserialize, Serialize, Validate, Debug)] +pub struct Listen { + pub host: Option, + pub port: u16, +} + +#[derive(Deserialize, Serialize, Validate, Debug)] +pub struct Proxy { + pub enabled: bool, + #[validate(url)] + pub url: String, + pub allowlist: Option>, } #[derive(Deserialize, Serialize, Validate, Debug)] @@ -44,7 +77,7 @@ pub struct Info { } #[derive(Deserialize, Serialize, Validate, Debug)] -pub struct Timelines { +pub struct Timeline { /// Whether to enable the local timeline pub local: bool, /// Whether to enable the global timeline @@ -55,13 +88,6 @@ pub struct Timelines { pub guest: bool, } -#[derive(Deserialize, Serialize, Validate, Debug)] -pub struct Network { - pub protocol: Option, - pub host: String, - pub port: u16, -} - #[derive(Deserialize, Serialize, Debug)] #[serde(rename_all = "lowercase")] pub enum HttpProtocol { @@ -95,3 +121,11 @@ pub struct Id { pub length: Option, pub fingerprint: Option, } + +#[derive(Deserialize, Serialize, Validate, Debug)] +pub struct File { + /// Maximum file size in megabytes + pub max_size: Option, + pub proxy_remote_file: Option, + pub cache_remote_file: Option, +}