summaryrefslogtreecommitdiff
path: root/libs/shared/src/config.rs
diff options
context:
space:
mode:
Diffstat (limited to 'libs/shared/src/config.rs')
-rw-r--r--libs/shared/src/config.rs37
1 files changed, 12 insertions, 25 deletions
diff --git a/libs/shared/src/config.rs b/libs/shared/src/config.rs
index 52137a3..4387dfb 100644
--- a/libs/shared/src/config.rs
+++ b/libs/shared/src/config.rs
@@ -1,17 +1,11 @@
-use std::env;
+use std::{env, ops::Deref};
use config::{Config, Environment, File};
use log::info;
-use serde::Deserialize;
+use serde::{Deserialize, de::DeserializeOwned};
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> {
+pub struct Settings<T: Clone + DeserializeOwned + Default> {
#[serde(skip_deserializing)]
pub config: T,
pub monitoring: crate::monitoring::MonitoringConfiguration,
@@ -19,20 +13,11 @@ pub struct Settings<T> {
pub redis: crate::redis::RedisConfiguration,
}
-///
-impl<T> Settings<T>
-where
- T: Deserialize<'static> + std::default::Default + Clone,
+impl<'de, T: Clone + DeserializeOwned + Default> Settings<T>
{
-
- /// 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);
@@ -49,13 +34,15 @@ where
// 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();
-}
+impl<T: Clone + DeserializeOwned + Default> Deref for Settings<T> {
+ type Target = T;
+
+ fn deref(&self) -> &Self::Target {
+ &self.config
+ }
+} \ No newline at end of file