summaryrefslogtreecommitdiff
path: root/common/rust/src/config.rs
blob: d4437a95b50faf3092849e442e984f482c6bdaeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use std::env;

use config::{Config, ConfigError, Environment, File, Source};
use log::info;
use serde::Deserialize;

#[derive(Debug, Deserialize, Clone)]
#[serde(bound(deserialize = "T: Deserialize<'de> + std::default::Default + Clone"))]
pub struct Settings<T> {
    #[serde(skip_deserializing)]
    pub config: T,
    pub monitoring: crate::monitoring::MonitoringConfiguration,
    pub nats: crate::nats::NatsConfiguration,
}

impl<T> Settings<T>
where
    T: Deserialize<'static> + std::default::Default + Clone,
{
    pub fn new(service_name: &str) -> Result<Settings<T>, ConfigError> {
        let mut default = Config::default();
        // this file my be shared with all the components
        default.merge(File::with_name("config/default"))?;
        let mode = env::var("ENV").unwrap_or_else(|_| "development".into());
        info!("Configuration Environment: {}", mode);

        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("__");
        println!("{:?}", env.collect());
        // we can configure each component using environment variables
        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)
    }
}