Skip to content

Commit

Permalink
refactor: [#596] move env var constants to config mod
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed May 22, 2024
1 parent b8d0f98 commit 5910fda
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 46 deletions.
22 changes: 1 addition & 21 deletions src/bootstrap/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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()
}
Expand Down
37 changes: 21 additions & 16 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<Self, Error> {
pub fn new(default_config_toml_path: String) -> Result<Self, Error> {
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)
Expand Down
10 changes: 1 addition & 9 deletions tests/e2e/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit 5910fda

Please sign in to comment.