diff options
Diffstat (limited to 'common/rust/src')
| -rw-r--r-- | common/rust/src/config.rs | 10 | ||||
| -rw-r--r-- | common/rust/src/lib.rs | 7 | ||||
| -rw-r--r-- | common/rust/src/monitoring.rs | 35 | ||||
| -rw-r--r-- | common/rust/src/nats.rs | 34 | ||||
| -rw-r--r-- | common/rust/src/payloads.rs | 2 |
5 files changed, 49 insertions, 39 deletions
diff --git a/common/rust/src/config.rs b/common/rust/src/config.rs index 3dcd72c..c4ad7b0 100644 --- a/common/rust/src/config.rs +++ b/common/rust/src/config.rs @@ -1,5 +1,5 @@ use std::env; -use config::{Config, ConfigError, Environment, File, Source}; +use config::{Config, ConfigError, Environment, File}; use log::info; use serde::Deserialize; @@ -24,6 +24,8 @@ where /// 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"))?; @@ -34,17 +36,19 @@ where 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) } } + +pub fn test_init() { + pretty_env_logger::init(); +} diff --git a/common/rust/src/lib.rs b/common/rust/src/lib.rs index 1125f5a..be3c913 100644 --- a/common/rust/src/lib.rs +++ b/common/rust/src/lib.rs @@ -5,3 +5,10 @@ pub mod monitoring; pub mod nats; pub mod payloads; pub mod error; + +pub use log as log; +pub use serde as serde; +pub use ::config as config_crate; +pub use prometheus as prometheus; +pub use ::nats as nats_crate; +pub use testcontainers as testcontainers;
\ No newline at end of file diff --git a/common/rust/src/monitoring.rs b/common/rust/src/monitoring.rs index ded1d95..4bff043 100644 --- a/common/rust/src/monitoring.rs +++ b/common/rust/src/monitoring.rs @@ -1,19 +1,19 @@ use hyper::{ - Response, Body, Request, Server, - header::{CONTENT_TYPE}, + header::CONTENT_TYPE, service::{make_service_fn, service_fn}, + Body, Request, Response, Server, }; -use std::net::ToSocketAddrs; +use log::{error, info}; use prometheus::{Encoder, TextEncoder}; -use log::{info,error}; use serde::Deserialize; +use std::net::ToSocketAddrs; #[derive(Clone, Debug, Deserialize)] /// Options for the monitoring service pub struct MonitoringConfiguration { - enabled: bool, - address: Option<String>, - port: Option<i32>, + pub enabled: bool, + pub address: Option<String>, + pub port: Option<i32>, } /// Handler for the hyper http server @@ -37,17 +37,18 @@ pub fn start_monitoring(configuration: &MonitoringConfiguration) { let config = configuration.clone(); tokio::task::spawn(async move { if config.enabled { - let address = format!("{}:{}", - config.address.expect("a listening address must be specified for the metrics server"), - config.port.expect("a listening port must be specified for the metrics server") + let address = format!( + "{}:{}", + config + .address + .expect("a listening address must be specified for the metrics server"), + config + .port + .expect("a listening port must be specified for the metrics server") ); info!("Starting monitoring server on {}", address); - - let listen_address = address - .to_socket_addrs() - .unwrap() - .next() - .unwrap(); + + let listen_address = address.to_socket_addrs().unwrap().next().unwrap(); let server = Server::bind(&listen_address).serve(make_service_fn(|_| async { Ok::<_, hyper::Error>(service_fn(serve_metrics)) })); @@ -57,4 +58,4 @@ pub fn start_monitoring(configuration: &MonitoringConfiguration) { } } }); -}
\ No newline at end of file +} diff --git a/common/rust/src/nats.rs b/common/rust/src/nats.rs index 437a857..c61aa4c 100644 --- a/common/rust/src/nats.rs +++ b/common/rust/src/nats.rs @@ -1,33 +1,34 @@ -use nats::{Options, Connection}; +use nats::{Connection, Options}; use serde::Deserialize; #[derive(Clone, Debug, Deserialize)] -struct NatsConfigurationClientCert { - cert: String, - key: String +pub struct NatsConfigurationClientCert { + pub cert: String, + pub key: String, } #[derive(Clone, Debug, Deserialize)] -struct NatsConfigurationTls { - mtu: Option<usize>, +pub struct NatsConfigurationTls { + pub mtu: Option<usize>, } #[derive(Clone, Debug, Deserialize)] pub struct NatsConfiguration { - client_cert: Option<NatsConfigurationClientCert>, - root_cert: Option<Vec<String>>, - jetstream_api_prefix: Option<String>, - max_reconnects: Option<usize>, - reconnect_buffer_size: Option<usize>, - tls: Option<NatsConfigurationTls>, - client_name: Option<String>, - tls_required: Option<bool>, - host: String, + pub client_cert: Option<NatsConfigurationClientCert>, + pub root_cert: Option<Vec<String>>, + pub jetstream_api_prefix: Option<String>, + pub max_reconnects: Option<usize>, + pub reconnect_buffer_size: Option<usize>, + pub tls: Option<NatsConfigurationTls>, + pub client_name: Option<String>, + pub tls_required: Option<bool>, + pub host: String, } +// Allows the configuration to directly create a nats connection impl Into<Connection> for NatsConfiguration { fn into(self) -> Connection { let mut options = Options::new(); - + if let Some(client_cert) = self.client_cert { options = options.client_cert(client_cert.cert, client_cert.key); } @@ -48,7 +49,6 @@ impl Into<Connection> for NatsConfiguration { options = options.tls_required(self.tls_required.unwrap_or(false)); options = options.with_name(&self.client_name.unwrap_or("Nova".to_string())); - if let Some(tls) = self.tls { let mut config = nats::rustls::ClientConfig::new(); config.set_mtu(&tls.mtu); diff --git a/common/rust/src/payloads.rs b/common/rust/src/payloads.rs index 2da70e3..c97ac1d 100644 --- a/common/rust/src/payloads.rs +++ b/common/rust/src/payloads.rs @@ -1,8 +1,6 @@ use serde::{Deserialize, Serialize}; use std::fmt::Debug; - - /// Data structure sent to the cache component /// by the gateway & webhook. #[derive(Serialize, Deserialize, Debug)] |
