From 5910fda5662e6b3054e6d4be62a8b67a2e11709e Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Wed, 22 May 2024 08:15:54 +0100 Subject: [PATCH] refactor: [#596] move env var constants to config mod --- src/bootstrap/config.rs | 22 +--------------------- src/config/mod.rs | 37 +++++++++++++++++++++---------------- tests/e2e/config.rs | 10 +--------- 3 files changed, 23 insertions(+), 46 deletions(-) diff --git a/src/bootstrap/config.rs b/src/bootstrap/config.rs index b98bb42e..c14454ab 100644 --- a/src/bootstrap/config.rs +++ b/src/bootstrap/config.rs @@ -6,19 +6,6 @@ use crate::config::{Configuration, Info}; -/// The whole `index.toml` file content. It has priority over the config file. -/// Even if the file is not on the default path. -pub const ENV_VAR_CONFIG: &str = "TORRUST_INDEX_CONFIG"; - -/// Token needed to communicate with the Torrust Tracker -pub const ENV_VAR_API_ADMIN_TOKEN: &str = "TORRUST_INDEX_TRACKER_API_TOKEN"; - -/// Secret key used to encrypt and decrypt -pub const ENV_VAR_AUTH_SECRET_KEY: &str = "TORRUST_INDEX_AUTH_SECRET_KEY"; - -/// The `index.toml` file location. -pub const ENV_VAR_PATH_CONFIG: &str = "TORRUST_INDEX_PATH_CONFIG"; - // Default values pub const DEFAULT_PATH_CONFIG: &str = "./share/default/config/index.development.sqlite3.toml"; @@ -42,14 +29,7 @@ pub const ENV_VAR_CORS_PERMISSIVE: &str = "TORRUST_INDEX_API_CORS_PERMISSIVE"; /// `./index.toml` file or the env var `TORRUST_INDEX_CONFIG`. #[must_use] pub fn initialize_configuration() -> Configuration { - let info = Info::new( - ENV_VAR_CONFIG.to_string(), - ENV_VAR_PATH_CONFIG.to_string(), - DEFAULT_PATH_CONFIG.to_string(), - ENV_VAR_API_ADMIN_TOKEN.to_string(), - ENV_VAR_AUTH_SECRET_KEY.to_string(), - ) - .unwrap(); + let info = Info::new(DEFAULT_PATH_CONFIG.to_string()).unwrap(); Configuration::load(&info).unwrap() } diff --git a/src/config/mod.rs b/src/config/mod.rs index cf60b902..10648333 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -31,9 +31,23 @@ pub type EmailOnSignup = v1::auth::EmailOnSignup; /// Prefix for env vars that overwrite configuration options. const CONFIG_OVERRIDE_PREFIX: &str = "TORRUST_INDEX_CONFIG_OVERRIDE_"; + /// Path separator in env var names for nested values in configuration. const CONFIG_OVERRIDE_SEPARATOR: &str = "__"; +/// The whole `index.toml` file content. It has priority over the config file. +/// Even if the file is not on the default path. +pub const ENV_VAR_CONFIG: &str = "TORRUST_INDEX_CONFIG"; + +/// Token needed to communicate with the Torrust Tracker +pub const ENV_VAR_API_ADMIN_TOKEN: &str = "TORRUST_INDEX_TRACKER_API_TOKEN"; + +/// Secret key used to encrypt and decrypt +pub const ENV_VAR_AUTH_SECRET_KEY: &str = "TORRUST_INDEX_AUTH_SECRET_KEY"; + +/// The `index.toml` file location. +pub const ENV_VAR_PATH_CONFIG: &str = "TORRUST_INDEX_PATH_CONFIG"; + /// Information required for loading config #[derive(Debug, Default, Clone)] pub struct Info { @@ -44,28 +58,19 @@ pub struct Info { } impl Info { - /// Build Configuration Info - /// - /// # Examples - /// - /// ```no_run - /// # use torrust_index::config::Info; - /// # let (env_var_config, env_var_path_config, default_path_config, env_var_tracker_api_token, env_var_auth_secret_key) = ("".to_string(), "".to_string(), "".to_string(), "".to_string(), "".to_string()); - /// let result = Info::new(env_var_config, env_var_path_config, default_path_config, env_var_tracker_api_token, env_var_auth_secret_key); - /// ``` + /// Build configuration Info. /// /// # Errors /// /// Will return `Err` if unable to obtain a configuration. /// #[allow(clippy::needless_pass_by_value)] - pub fn new( - env_var_config_toml: String, - env_var_config_toml_path: String, - default_config_toml_path: String, - env_var_tracker_api_token: String, - env_var_auth_secret_key: String, - ) -> Result { + pub fn new(default_config_toml_path: String) -> Result { + let env_var_config_toml = ENV_VAR_CONFIG.to_string(); + let env_var_config_toml_path = ENV_VAR_PATH_CONFIG.to_string(); + let env_var_tracker_api_token = ENV_VAR_API_ADMIN_TOKEN.to_string(); + let env_var_auth_secret_key = ENV_VAR_AUTH_SECRET_KEY.to_string(); + let config_toml = if let Ok(config_toml) = env::var(env_var_config_toml) { println!("Loading configuration from environment variable {config_toml} ..."); Some(config_toml) diff --git a/tests/e2e/config.rs b/tests/e2e/config.rs index 61a21b4d..09e61003 100644 --- a/tests/e2e/config.rs +++ b/tests/e2e/config.rs @@ -5,7 +5,6 @@ // Environment variables -use torrust_index::bootstrap::config::{ENV_VAR_API_ADMIN_TOKEN, ENV_VAR_AUTH_SECRET_KEY, ENV_VAR_CONFIG, ENV_VAR_PATH_CONFIG}; use torrust_index::config::{Configuration, Info}; // Default values @@ -34,14 +33,7 @@ pub const ENV_VAR_DB_CONNECT_URL: &str = "TORRUST_INDEX_E2E_DB_CONNECT_URL"; /// `./index.toml` file or the env var `TORRUST_INDEX_CONFIG`. #[must_use] pub fn initialize_configuration() -> Configuration { - let info = Info::new( - ENV_VAR_CONFIG.to_string(), - ENV_VAR_PATH_CONFIG.to_string(), - DEFAULT_PATH_CONFIG.to_string(), - ENV_VAR_API_ADMIN_TOKEN.to_string(), - ENV_VAR_AUTH_SECRET_KEY.to_string(), - ) - .unwrap(); + let info = Info::new(DEFAULT_PATH_CONFIG.to_string()).unwrap(); Configuration::load(&info).unwrap() }