diff options
Diffstat (limited to 'common/rust/src/config.rs')
| -rw-r--r-- | common/rust/src/config.rs | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/common/rust/src/config.rs b/common/rust/src/config.rs index c158a21..e4dbade 100644 --- a/common/rust/src/config.rs +++ b/common/rust/src/config.rs @@ -1,10 +1,12 @@ use std::env; - use config::{Config, ConfigError, Environment, File}; use log::info; -use serde::{Deserialize}; - +use serde::Deserialize; +/// Settings<T> is the base structure for all the nova's component config +/// you can specify a type T and the name of the component. the "config" +/// field will be equals to the key named after the given component name +/// and will be of type T #[derive(Debug, Deserialize, Clone)] #[serde(bound(deserialize = "T: Deserialize<'de> + std::default::Default + Clone"))] pub struct Settings<T> { @@ -12,10 +14,19 @@ pub struct Settings<T> { pub config: T, pub monitoring: crate::monitoring::MonitoringConfiguration, pub nats: crate::nats::NatsConfiguration, + pub redis: crate::redis::RedisConfiguration, } -impl<T> Settings<T> where T: Deserialize<'static> + std::default::Default + Clone { +/// +impl<T> Settings<T> +where + T: Deserialize<'static> + std::default::Default + Clone, +{ + /// Initializes a new configuration like the other components of nova + /// And starts the prometheus metrics server if needed. pub fn new(service_name: &str) -> Result<Settings<T>, ConfigError> { + pretty_env_logger::init(); + let mut default = Config::default(); // this file my be shared with all the components default.merge(File::with_name("config/default"))?; @@ -25,16 +36,20 @@ impl<T> Settings<T> where T: Deserialize<'static> + std::default::Default + Clon default.merge(File::with_name(&format!("config/{}", mode)).required(false))?; default.merge(File::with_name("config/local").required(false))?; + let env = Environment::with_prefix("NOVA").separator("__"); // we can configure each component using environment variables - default.merge(Environment::with_prefix("NOVA").separator("_"))?; + default.merge(env)?; let mut config: Settings<T> = default.clone().try_into().unwrap(); // try to load the config config.config = default.get::<T>(&service_name).unwrap(); - pretty_env_logger::init(); // start the monitoring system if needed crate::monitoring::start_monitoring(&config.monitoring); Ok(config) } } + +pub fn test_init() { + pretty_env_logger::init(); +} |
