Skip to content

Commit

Permalink
refactor: [#361] extract mod for configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Aug 8, 2023
1 parent 5a0441b commit 8a79fbe
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
31 changes: 1 addition & 30 deletions src/bootstrap/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
//! 2. Initialize static variables.
//! 3. Initialize logging.
//! 4. Initialize the domain tracker.
use std::env;
use std::sync::Arc;

use torrust_tracker_configuration::Configuration;

use super::config::initialize_configuration;
use crate::bootstrap;
use crate::shared::clock::static_time;
use crate::shared::crypto::ephemeral_instance_keys;
Expand Down Expand Up @@ -55,35 +55,6 @@ pub fn initialize_static() {
lazy_static::initialize(&ephemeral_instance_keys::RANDOM_SEED);
}

/// It loads the application configuration from the environment.
///
/// There are two methods to inject the configuration:
///
/// 1. By using a config file: `config.toml`. The file must be in the same folder where you are running the tracker.
/// 2. Environment variable: `TORRUST_TRACKER_CONFIG`. The variable contains the same contents as the `config.toml` file.
///
/// Environment variable has priority over the config file.
///
/// Refer to the [configuration documentation](https://docs.rs/torrust-tracker-configuration) for the configuration options.
///
/// # Panics
///
/// Will panic if it can't load the configuration from either
/// `./config.toml` file or the env var `TORRUST_TRACKER_CONFIG`.
#[must_use]
fn initialize_configuration() -> Configuration {
const CONFIG_PATH: &str = "./config.toml";
const CONFIG_ENV_VAR_NAME: &str = "TORRUST_TRACKER_CONFIG";

if env::var(CONFIG_ENV_VAR_NAME).is_ok() {
println!("Loading configuration from env var {CONFIG_ENV_VAR_NAME}");
Configuration::load_from_env_var(CONFIG_ENV_VAR_NAME).unwrap()
} else {
println!("Loading configuration from config file {CONFIG_PATH}");
Configuration::load_from_file(CONFIG_PATH).unwrap()
}
}

/// It builds the domain tracker
///
/// The tracker is the domain layer service. It's the entrypoint to make requests to the domain layer.
Expand Down
37 changes: 37 additions & 0 deletions src/bootstrap/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//! Initialize configuration from file or env var.
//!
//! All environment variables are prefixed with `TORRUST_TRACKER_BACK_`.
use std::env;

use torrust_tracker_configuration::Configuration;

// Environment variables

const CONFIG_PATH: &str = "./config.toml";
const CONFIG_ENV_VAR_NAME: &str = "TORRUST_TRACKER_CONFIG";

/// It loads the application configuration from the environment.
///
/// There are two methods to inject the configuration:
///
/// 1. By using a config file: `config.toml`. The file must be in the same folder where you are running the tracker.
/// 2. Environment variable: `TORRUST_TRACKER_CONFIG`. The variable contains the same contents as the `config.toml` file.
///
/// Environment variable has priority over the config file.
///
/// Refer to the [configuration documentation](https://docs.rs/torrust-tracker-configuration) for the configuration options.
///
/// # Panics
///
/// Will panic if it can't load the configuration from either
/// `./config.toml` file or the env var `TORRUST_TRACKER_CONFIG`.
#[must_use]
pub fn initialize_configuration() -> Configuration {
if env::var(CONFIG_ENV_VAR_NAME).is_ok() {
println!("Loading configuration from env var {CONFIG_ENV_VAR_NAME}");
Configuration::load_from_env_var(CONFIG_ENV_VAR_NAME).unwrap()

Check warning on line 32 in src/bootstrap/config.rs

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/config.rs#L29-L32

Added lines #L29 - L32 were not covered by tests
} else {
println!("Loading configuration from config file {CONFIG_PATH}");
Configuration::load_from_file(CONFIG_PATH).unwrap()

Check warning on line 35 in src/bootstrap/config.rs

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/config.rs#L34-L35

Added lines #L34 - L35 were not covered by tests
}
}

Check warning on line 37 in src/bootstrap/config.rs

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/config.rs#L37

Added line #L37 was not covered by tests
1 change: 1 addition & 0 deletions src/bootstrap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
//! like cleaning torrents, and other jobs because they can be enabled/disabled depending on the configuration.
//! For example, you can have more than one UDP and HTTP tracker, each server is executed like a independent job.
pub mod app;
pub mod config;
pub mod jobs;
pub mod logging;

0 comments on commit 8a79fbe

Please sign in to comment.