From a6ed29b5a1d062212a9e2c67536b600bf91384c9 Mon Sep 17 00:00:00 2001 From: Serge Farny Date: Fri, 1 Mar 2024 17:07:49 +0100 Subject: [PATCH] service-mango-health: read env variable for more configuration entries (#901) (cherry picked from commit af9b21c430b8afd962f34e05708275ee3d130787) --- .../src/postgres_config.rs | 2 ++ bin/service-mango-health/src/configuration.rs | 4 ++++ lib/services-mango-lib/src/env_helper.rs | 21 +++++++++++++++++++ lib/services-mango-lib/src/lib.rs | 1 + .../src/postgres_configuration.rs | 2 ++ .../src/postgres_connection.rs | 8 +------ 6 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 lib/services-mango-lib/src/env_helper.rs diff --git a/bin/service-mango-fills/src/postgres_config.rs b/bin/service-mango-fills/src/postgres_config.rs index 35342673a..e238c23de 100644 --- a/bin/service-mango-fills/src/postgres_config.rs +++ b/bin/service-mango-fills/src/postgres_config.rs @@ -1,8 +1,10 @@ use serde_derive::Deserialize; +use services_mango_lib::env_helper::string_or_env; use services_mango_lib::postgres_configuration::PostgresTlsConfig; #[derive(Clone, Debug, Deserialize)] pub struct PostgresConfig { + #[serde(deserialize_with = "string_or_env")] pub connection_string: String, /// Number of parallel postgres connections used for insertions pub connection_count: u64, diff --git a/bin/service-mango-health/src/configuration.rs b/bin/service-mango-health/src/configuration.rs index 9552898b9..76ca7944d 100644 --- a/bin/service-mango-health/src/configuration.rs +++ b/bin/service-mango-health/src/configuration.rs @@ -1,12 +1,16 @@ use serde_derive::Deserialize; +use services_mango_lib::env_helper::string_or_env; use services_mango_lib::postgres_configuration::PostgresConfiguration; use std::collections::HashSet; #[derive(Clone, Debug, Deserialize)] pub struct Configuration { pub postgres: Option, + #[serde(deserialize_with = "string_or_env")] pub rpc_http_url: String, + #[serde(deserialize_with = "string_or_env")] pub rpc_ws_url: String, + #[serde(deserialize_with = "string_or_env")] pub mango_group: String, pub computing_configuration: ComputingConfiguration, pub logging_configuration: LoggingConfiguration, diff --git a/lib/services-mango-lib/src/env_helper.rs b/lib/services-mango-lib/src/env_helper.rs new file mode 100644 index 000000000..242262ad1 --- /dev/null +++ b/lib/services-mango-lib/src/env_helper.rs @@ -0,0 +1,21 @@ +use serde::{Deserialize, Deserializer}; +use std::env; + +/// Get a string content, or the content of an Env variable it the string start with $ +/// +/// Example: +/// - "abc" -> "abc" +/// - "$something" -> read env variable named something and return it's content +/// +/// *WARNING*: May kill the program if we are asking for anv environment variable that does not exist +pub fn string_or_env<'de, D>(deserializer: D) -> Result +where + D: Deserializer<'de>, +{ + let value_or_env = String::deserialize(deserializer)?; + let value = match &value_or_env.chars().next().unwrap() { + '$' => env::var(&value_or_env[1..]).expect("reading from env"), + _ => value_or_env, + }; + Ok(value) +} diff --git a/lib/services-mango-lib/src/lib.rs b/lib/services-mango-lib/src/lib.rs index 36329b591..211b75722 100644 --- a/lib/services-mango-lib/src/lib.rs +++ b/lib/services-mango-lib/src/lib.rs @@ -1,3 +1,4 @@ +pub mod env_helper; pub mod postgres_configuration; pub mod postgres_connection; pub mod retry_counter; diff --git a/lib/services-mango-lib/src/postgres_configuration.rs b/lib/services-mango-lib/src/postgres_configuration.rs index 85f8f6e75..718f3a5ea 100644 --- a/lib/services-mango-lib/src/postgres_configuration.rs +++ b/lib/services-mango-lib/src/postgres_configuration.rs @@ -1,7 +1,9 @@ +use crate::env_helper::string_or_env; use serde::Deserialize; #[derive(Clone, Debug, Deserialize, Default)] pub struct PostgresConfiguration { + #[serde(deserialize_with = "string_or_env")] pub connection_string: String, pub allow_invalid_certs: bool, pub tls: Option, diff --git a/lib/services-mango-lib/src/postgres_connection.rs b/lib/services-mango-lib/src/postgres_connection.rs index def70aa49..bc393ac88 100644 --- a/lib/services-mango-lib/src/postgres_connection.rs +++ b/lib/services-mango-lib/src/postgres_connection.rs @@ -51,14 +51,8 @@ pub async fn connect( }; let config = config.clone(); - let connection_string = match &config.connection_string.chars().next().unwrap() { - '$' => { - env::var(&config.connection_string[1..]).expect("reading connection string from env") - } - _ => config.connection_string.clone(), - }; - let (client, connection) = tokio_postgres::connect(&connection_string, tls).await?; + let (client, connection) = tokio_postgres::connect(&config.connection_string, tls).await?; let handle = tokio::spawn(async move { connection.await });