From 8ed77d7ab484121e9d70158e14c9fd6c243f1c70 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Mon, 12 Feb 2024 14:58:08 +0100 Subject: Close #9. Config impl with struct and files --- src/config.rs | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index 4c79810..e88ddab 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,11 +1,25 @@ -use config::Config; -use once_cell::sync::Lazy; +use config::File; +use serde::Deserialize; -pub static SETTINGS: Lazy = Lazy::new(setup); +#[derive(Debug, Clone, Deserialize)] +pub struct Config { + pub database_url: String, + pub apikey: String, + pub serveraddr: String, + pub pingtimeout: i64, +} + +impl Config { + pub fn load() -> Result { + let config = config::Config::builder() + .set_default("serveraddr", "0.0.0.0:7229")? + .set_default("pingtimeout", 10)? + .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()?; + + config.try_deserialize() + } +} -fn setup() -> Config { - Config::builder() - .add_source(config::Environment::with_prefix("WEBOL").separator("_")) - .build() - .unwrap() -} \ No newline at end of file -- cgit v1.2.3