blob: 52137a3fd7badfdbaaa352e2338f9618559bb460 (
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
use std::env;
use config::{Config, Environment, File};
use log::info;
use serde::Deserialize;
use crate::error::GenericError;
/// 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> {
#[serde(skip_deserializing)]
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,
{
/// 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>, GenericError> {
pretty_env_logger::init();
let mut builder = Config::builder();
// this file my be shared with all the components
builder = builder.add_source(File::with_name("config/default"));
let mode = env::var("ENV").unwrap_or_else(|_| "development".into());
info!("Configuration Environment: {}", mode);
builder = builder.add_source(File::with_name(&format!("config/{}", mode)).required(false));
builder = builder.add_source(File::with_name("config/local").required(false));
let env = Environment::with_prefix("NOVA").separator("__");
// we can configure each component using environment variables
builder = builder.add_source(env);
let config = builder.build()?;
let mut settings: Settings<T> = config.clone().try_deserialize()?;
// try to load the config
settings.config = config.get::<T>(service_name)?;
// start the monitoring system if needed
crate::monitoring::start_monitoring(&settings.monitoring);
Ok(settings)
}
}
pub fn test_init() {
pretty_env_logger::init();
}
|