summaryrefslogtreecommitdiff
path: root/libs/shared/src
diff options
context:
space:
mode:
authorMatthieuCoder <matthieu@matthieu-dev.xyz>2023-01-02 18:59:03 +0400
committerMatthieuCoder <matthieu@matthieu-dev.xyz>2023-01-02 18:59:03 +0400
commitf8c2a144e2f3e47371f5e8352e7a7a0b6707bf88 (patch)
tree8c1e6bd157ac599429c806f9aa9bc9dbc28140ed /libs/shared/src
parent46fd26962ef55f8b557f7e36d3aee915a819c88c (diff)
restructure project
Diffstat (limited to 'libs/shared/src')
-rw-r--r--libs/shared/src/config.rs37
-rw-r--r--libs/shared/src/nats.rs15
-rw-r--r--libs/shared/src/redis.rs19
3 files changed, 35 insertions, 36 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
diff --git a/libs/shared/src/nats.rs b/libs/shared/src/nats.rs
index 05953cc..dc922d5 100644
--- a/libs/shared/src/nats.rs
+++ b/libs/shared/src/nats.rs
@@ -1,8 +1,7 @@
+use std::{future::Future, pin::Pin};
+
use async_nats::Client;
use serde::Deserialize;
-use std::future::Future;
-
-use crate::error::GenericError;
#[derive(Clone, Debug, Deserialize)]
pub struct NatsConfigurationClientCert {
@@ -20,10 +19,8 @@ pub struct NatsConfiguration {
pub host: String,
}
-// todo: Prefer From since it automatically gives a free Into implementation
-// Allows the configuration to directly create a nats connection
-impl NatsConfiguration {
- pub async fn to_client(self) -> Result<Client, GenericError> {
- Ok(async_nats::connect(self.host).await?)
+impl From<NatsConfiguration> for Pin<Box<dyn Future<Output = anyhow::Result<Client>>>> {
+ fn from(value: NatsConfiguration) -> Self {
+ Box::pin(async move { Ok(async_nats::connect(value.host).await?) })
}
-} \ No newline at end of file
+}
diff --git a/libs/shared/src/redis.rs b/libs/shared/src/redis.rs
index a196f8d..5753fb6 100644
--- a/libs/shared/src/redis.rs
+++ b/libs/shared/src/redis.rs
@@ -1,6 +1,6 @@
-use redis::Client;
+use redis::{aio::MultiplexedConnection, Client};
use serde::Deserialize;
-
+use std::{future::Future, pin::Pin};
#[derive(Clone, Debug, Deserialize)]
pub struct RedisConfiguration {
@@ -13,3 +13,18 @@ impl Into<Client> for RedisConfiguration {
redis::Client::open(self.url).unwrap()
}
}
+
+impl From<RedisConfiguration>
+ for Pin<Box<dyn Future<Output = anyhow::Result<MultiplexedConnection>>>>
+{
+ fn from(value: RedisConfiguration) -> Self {
+ Box::pin(async move {
+ let con = Client::open(value.url)?;
+ let (multiplex, ready) = con.create_multiplexed_tokio_connection().await?;
+
+ tokio::spawn(ready);
+
+ Ok(multiplex)
+ })
+ }
+}