use config::Config; use lazy_static::lazy_static; use serde_derive::Deserialize; use std::sync::RwLock; #[derive(Clone, Debug, Deserialize)] #[serde(default)] #[allow(unused)] pub struct Logging { pub level: String, pub file: String, } #[derive(Clone, Debug, Deserialize)] #[serde(default)] #[allow(unused)] pub struct Settings { pub database: String, pub port: u16, pub host: String, pub logging: Logging, } impl Default for Settings { fn default() -> Self { Settings { database: String::from("sqlite://emgauwa-core.sqlite"), port: 5000, host: String::from("127.0.0.1"), logging: Logging::default(), } } } impl Default for Logging { fn default() -> Self { Logging { level: String::from("info"), file: String::from("stdout"), } } } lazy_static! { static ref SETTINGS: RwLock = RwLock::new(Settings::default()); } pub fn init() { let settings = Config::builder() .add_source(config::File::with_name("emgauwa-core")) .add_source( config::Environment::with_prefix("EMGAUWA") .prefix_separator("_") .separator("__"), ) .build() .unwrap() .try_deserialize::() .unwrap_or_else(|_| panic!("Error reading settings.")); *SETTINGS.write().unwrap() = settings; } pub fn get() -> Settings { SETTINGS.read().unwrap().clone() }