aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfxqnlr <[email protected]>2024-10-04 17:14:23 +0200
committerfxqnlr <[email protected]>2024-10-04 17:14:23 +0200
commit0d5ac9f0ef306faa4e0ad5315fd6368400449d1a (patch)
tree634066ee92cf1ca9db33e99ffd45199b2fc437a0
parent3e8866c07fbd6c2819ff3833e52ede9158a9862d (diff)
downloadwebol-0d5ac9f0ef306faa4e0ad5315fd6368400449d1a.tar
webol-0d5ac9f0ef306faa4e0ad5315fd6368400449d1a.tar.gz
webol-0d5ac9f0ef306faa4e0ad5315fd6368400449d1a.zip
add default configuration through `Default`
-rw-r--r--src/config.rs29
1 files changed, 22 insertions, 7 deletions
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;
4use crate::auth; 4use crate::auth;
5 5
6#[derive(Debug, Clone, Deserialize)] 6#[derive(Debug, Clone, Deserialize)]
7#[serde(default)]
7pub struct Config { 8pub struct Config {
8 pub serveraddr: String, 9 pub serveraddr: String,
9 pub pingtimeout: i64, 10 pub pingtimeout: i64,
@@ -11,23 +12,37 @@ pub struct Config {
11 pub auth: Auth, 12 pub auth: Auth,
12} 13}
13 14
15impl Default for Config {
16 fn default() -> Self {
17 Self {
18 serveraddr: "0.0.0.0:7229".to_string(),
19 pingtimeout: 10,
20 pingthreshold: 1,
21 auth: Default::default(),
22 }
23 }
24}
25
14#[derive(Debug, Clone, Deserialize)] 26#[derive(Debug, Clone, Deserialize)]
15pub struct Auth { 27pub struct Auth {
16 pub method: auth::Methods, 28 pub method: auth::Methods,
17 pub secret: String, 29 pub secret: String,
18} 30}
19 31
32impl Default for Auth {
33 fn default() -> Self {
34 Self {
35 method: auth::Methods::None,
36 secret: String::new(),
37 }
38 }
39}
40
20impl Config { 41impl Config {
21 pub fn load() -> Result<Self, config::ConfigError> { 42 pub fn load() -> Result<Self, config::ConfigError> {
22 let config = config::Config::builder() 43 let config = config::Config::builder()
23 .set_default("serveraddr", "0.0.0.0:7229")? 44 .add_source(File::with_name("/etc/webol").required(false))
24 .set_default("pingtimeout", 10)?
25 .set_default("pingthreshold", 1)?
26 .set_default("timeoffset", 0)?
27 .set_default("auth.method", "none")?
28 .set_default("auth.secret", "")?
29 .add_source(File::with_name("config.toml").required(false)) 45 .add_source(File::with_name("config.toml").required(false))
30 .add_source(File::with_name("config.dev.toml").required(false))
31 .add_source(config::Environment::with_prefix("WEBOL").separator("_")) 46 .add_source(config::Environment::with_prefix("WEBOL").separator("_"))
32 .build()?; 47 .build()?;
33 48