summaryrefslogtreecommitdiff
path: root/common/rust/src
diff options
context:
space:
mode:
authorMatthieu <matthieu@developershouse.xyz>2021-09-09 22:16:39 +0400
committerMatthieu <matthieu@developershouse.xyz>2021-09-09 22:16:39 +0400
commit11912b050a97c258a8a38552d855f183c339beee (patch)
treed80f960beb4e0455cd8d0d8addb7b3308dda6933 /common/rust/src
parente28d134370196d3e4d3ff9016a36cce011031e58 (diff)
gateway improvements, common packages and examples
Diffstat (limited to 'common/rust/src')
-rw-r--r--common/rust/src/config.rs41
-rw-r--r--common/rust/src/lib.rs4
-rw-r--r--common/rust/src/monitoring.rs60
3 files changed, 105 insertions, 0 deletions
diff --git a/common/rust/src/config.rs b/common/rust/src/config.rs
new file mode 100644
index 0000000..6d8fb33
--- /dev/null
+++ b/common/rust/src/config.rs
@@ -0,0 +1,41 @@
+use std::env;
+
+use config::{Config, ConfigError, Environment, File};
+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,
+}
+
+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))?;
+
+ // we can configure each component using environment variables
+ default.merge(Environment::with_prefix(&format!("NOVA_{}", service_name)))?;
+ let mut config: Settings<T> = default.clone().try_into().unwrap();
+
+ // try to load the config
+ config.config = default.get::<T>(&service_name).unwrap();
+
+ // setup the logger
+ pretty_env_logger::init_custom_env(&format!("NOVA_{}_LOG", service_name));
+
+ // start the monitoring system if needed
+ crate::monitoring::start_monitoring(&config.monitoring);
+ Ok(config)
+ }
+}
diff --git a/common/rust/src/lib.rs b/common/rust/src/lib.rs
new file mode 100644
index 0000000..5122334
--- /dev/null
+++ b/common/rust/src/lib.rs
@@ -0,0 +1,4 @@
+/// This crate contains shared code in all the rust projects
+/// It contains utilities such as monitoring, logging and more.
+pub mod config;
+pub mod monitoring; \ No newline at end of file
diff --git a/common/rust/src/monitoring.rs b/common/rust/src/monitoring.rs
new file mode 100644
index 0000000..ded1d95
--- /dev/null
+++ b/common/rust/src/monitoring.rs
@@ -0,0 +1,60 @@
+use hyper::{
+ Response, Body, Request, Server,
+ header::{CONTENT_TYPE},
+ service::{make_service_fn, service_fn},
+};
+use std::net::ToSocketAddrs;
+use prometheus::{Encoder, TextEncoder};
+use log::{info,error};
+use serde::Deserialize;
+
+#[derive(Clone, Debug, Deserialize)]
+/// Options for the monitoring service
+pub struct MonitoringConfiguration {
+ enabled: bool,
+ address: Option<String>,
+ port: Option<i32>,
+}
+
+/// Handler for the hyper http server
+async fn serve_metrics(_request: Request<Body>) -> Result<Response<Body>, hyper::Error> {
+ let encoder = TextEncoder::new();
+ let metrics = prometheus::gather();
+
+ let mut buffer = vec![];
+ encoder.encode(&metrics, &mut buffer).unwrap();
+
+ let response = Response::builder()
+ .status(200)
+ .header(CONTENT_TYPE, encoder.format_type())
+ .body(Body::from(buffer))
+ .unwrap();
+ Ok(response)
+}
+
+/// Starts a monitoring server on the requested port
+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")
+ );
+ info!("Starting monitoring server on {}", address);
+
+ 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))
+ }));
+
+ if let Err(e) = server.await {
+ error!("failed to start the monitoring server {}", e);
+ }
+ }
+ });
+} \ No newline at end of file