service-mango-health: read env variable for more configuration entries (#901)

This commit is contained in:
Serge Farny 2024-03-01 17:07:49 +01:00 committed by GitHub
parent 53517f876b
commit af9b21c430
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 31 additions and 7 deletions

View File

@ -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,

View File

@ -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<PostgresConfiguration>,
#[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,

View File

@ -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<String, D::Error>
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)
}

View File

@ -1,3 +1,4 @@
pub mod env_helper;
pub mod postgres_configuration;
pub mod postgres_connection;
pub mod retry_counter;

View File

@ -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<PostgresTlsConfig>,

View File

@ -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 });