This commit is contained in:
naskya 2024-06-23 16:57:36 +09:00
parent f29f87fb37
commit d851dbbea6
Signed by: naskya
GPG key ID: 712D413B3A9FED5C
2 changed files with 125 additions and 46 deletions

View file

@ -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<server::Id> = 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)
}

View file

@ -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<Info>,
#[validate(nested)]
pub timelines: Option<Timelines>,
#[validate(nested)]
pub network: Network,
pub timeline: Option<Timeline>,
#[validate(nested)]
pub database: Database,
#[validate(nested)]
pub cache_server: CacheServer,
#[validate(nested)]
pub id: Option<Id>,
#[validate(nested)]
pub file: Option<File>,
}
#[derive(Deserialize, Serialize, Validate, Debug)]
pub struct Network {
pub protocol: Option<HttpProtocol>,
pub domain: String,
pub port: Option<u16>,
#[validate(nested)]
pub listen: Listen,
#[validate(nested)]
pub http_proxy: Option<Proxy>,
#[validate(nested)]
pub media_proxy: Option<Proxy>,
#[validate(nested)]
pub summaly_proxy: Option<Proxy>,
#[validate(nested)]
pub smtp_proxy: Option<Proxy>,
}
#[derive(Deserialize, Serialize, Validate, Debug)]
pub struct Listen {
pub host: Option<String>,
pub port: u16,
}
#[derive(Deserialize, Serialize, Validate, Debug)]
pub struct Proxy {
pub enabled: bool,
#[validate(url)]
pub url: String,
pub allowlist: Option<Vec<String>>,
}
#[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<HttpProtocol>,
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<u8>,
pub fingerprint: Option<String>,
}
#[derive(Deserialize, Serialize, Validate, Debug)]
pub struct File {
/// Maximum file size in megabytes
pub max_size: Option<u64>,
pub proxy_remote_file: Option<bool>,
pub cache_remote_file: Option<bool>,
}