Skip to content

Commit

Permalink
refactor: move config env vars to configuration package
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed May 14, 2024
1 parent da6a21e commit 4de5e7d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
24 changes: 18 additions & 6 deletions packages/configuration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ use torrust_tracker_located_error::{DynError, LocatedError};
/// The maximum number of returned peers for a torrent.
pub const TORRENT_PEERS_LIMIT: usize = 74;

// Environment variables

/// The whole `tracker.toml` file content. It has priority over the config file.
/// Even if the file is not on the default path.
const ENV_VAR_CONFIG: &str = "TORRUST_TRACKER_CONFIG";

/// The `tracker.toml` file location.
pub const ENV_VAR_PATH_CONFIG: &str = "TORRUST_TRACKER_PATH_CONFIG";

/// Env var to overwrite API admin token.
/// Deprecated: use `TORRUST_TRACKER_CONFIG_OVERRIDE_HTTP_API__ACCESS_TOKENS__ADMIN`.
const ENV_VAR_API_ADMIN_TOKEN: &str = "TORRUST_TRACKER_API_ADMIN_TOKEN";

pub type Configuration = v1::Configuration;
pub type UdpTracker = v1::udp_tracker::UdpTracker;
pub type HttpTracker = v1::http_tracker::HttpTracker;
Expand Down Expand Up @@ -51,12 +64,11 @@ impl Info {
/// 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_api_admin_token: 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_api_admin_token = ENV_VAR_API_ADMIN_TOKEN.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)

Check warning on line 74 in packages/configuration/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

packages/configuration/src/lib.rs#L73-L74

Added lines #L73 - L74 were not covered by tests
Expand Down
22 changes: 2 additions & 20 deletions src/bootstrap/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,6 @@

use torrust_tracker_configuration::{Configuration, Info};

// Environment variables

/// The whole `tracker.toml` file content. It has priority over the config file.
/// Even if the file is not on the default path.
const ENV_VAR_CONFIG: &str = "TORRUST_TRACKER_CONFIG";
const ENV_VAR_API_ADMIN_TOKEN: &str = "TORRUST_TRACKER_API_ADMIN_TOKEN";

/// The `tracker.toml` file location.
pub const ENV_VAR_PATH_CONFIG: &str = "TORRUST_TRACKER_PATH_CONFIG";

// Default values
pub const DEFAULT_PATH_CONFIG: &str = "./share/default/config/tracker.development.sqlite3.toml";

/// It loads the application configuration from the environment.
Expand All @@ -34,15 +23,8 @@ pub const DEFAULT_PATH_CONFIG: &str = "./share/default/config/tracker.developmen
/// `./tracker.toml` file or the env var `TORRUST_TRACKER_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(),
)
.unwrap();

Configuration::load(&info).unwrap()
let info = Info::new(DEFAULT_PATH_CONFIG.to_string()).expect("info to load configuration is not valid");
Configuration::load(&info).expect("configuration should be loaded from provided info")
}

#[cfg(test)]
Expand Down

0 comments on commit 4de5e7d

Please sign in to comment.