From 0d5ac9f0ef306faa4e0ad5315fd6368400449d1a Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Fri, 4 Oct 2024 17:14:23 +0200 Subject: add default configuration through `Default` --- src/config.rs | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index 18c3bab..4b333fe 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,6 +4,7 @@ use serde::Deserialize; use crate::auth; #[derive(Debug, Clone, Deserialize)] +#[serde(default)] pub struct Config { pub serveraddr: String, pub pingtimeout: i64, @@ -11,23 +12,37 @@ pub struct Config { pub auth: Auth, } +impl Default for Config { + fn default() -> Self { + Self { + serveraddr: "0.0.0.0:7229".to_string(), + pingtimeout: 10, + pingthreshold: 1, + auth: Default::default(), + } + } +} + #[derive(Debug, Clone, Deserialize)] pub struct Auth { pub method: auth::Methods, pub secret: String, } +impl Default for Auth { + fn default() -> Self { + Self { + method: auth::Methods::None, + secret: String::new(), + } + } +} + impl Config { pub fn load() -> Result { let config = config::Config::builder() - .set_default("serveraddr", "0.0.0.0:7229")? - .set_default("pingtimeout", 10)? - .set_default("pingthreshold", 1)? - .set_default("timeoffset", 0)? - .set_default("auth.method", "none")? - .set_default("auth.secret", "")? + .add_source(File::with_name("/etc/webol").required(false)) .add_source(File::with_name("config.toml").required(false)) - .add_source(File::with_name("config.dev.toml").required(false)) .add_source(config::Environment::with_prefix("WEBOL").separator("_")) .build()?; -- cgit v1.2.3 From ae386a491b23cfbadc4da8680c8ebcb85310dcb5 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Fri, 4 Oct 2024 17:23:09 +0200 Subject: rename `serveraddr` to `addr` --- src/config.rs | 4 ++-- src/main.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index 4b333fe..2e9c1eb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -6,7 +6,7 @@ use crate::auth; #[derive(Debug, Clone, Deserialize)] #[serde(default)] pub struct Config { - pub serveraddr: String, + pub addr: String, pub pingtimeout: i64, pub pingthreshold: u64, pub auth: Auth, @@ -15,7 +15,7 @@ pub struct Config { impl Default for Config { fn default() -> Self { Self { - serveraddr: "0.0.0.0:7229".to_string(), + addr: "0.0.0.0:7229".to_string(), pingtimeout: 10, pingthreshold: 1, auth: Default::default(), diff --git a/src/main.rs b/src/main.rs index b550dd8..352023b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -118,7 +118,7 @@ async fn main() -> color_eyre::eyre::Result<()> { .merge(SwaggerUi::new("/swagger-ui").url("/api-docs/openapi.json", ApiDoc::openapi())) .with_state(Arc::new(shared_state)); - let addr = config.serveraddr; + let addr = config.addr; info!("start server on {}", addr); let listener = tokio::net::TcpListener::bind(addr).await?; axum::serve(listener, app).await?; -- cgit v1.2.3