Skip to content

Commit

Permalink
feat: print the final configuration to console
Browse files Browse the repository at this point in the history
When the application starts, it only prints the loaded configuration when it's loaded from an env var. It's very helpful to print the final configuration after merging all sources with Figment.

default values -> merge with TOML file -> merge with env vars -> final configuration

It's printed in JSON format.
  • Loading branch information
josecelano committed Jul 2, 2024
1 parent bfd3f37 commit f39f5e5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::net::SocketAddr;
use std::sync::Arc;

use tokio::task::JoinHandle;
use tracing::info;

use crate::bootstrap::logging;
use crate::cache::image::manager::ImageCacheService;
Expand Down Expand Up @@ -42,6 +43,8 @@ pub async fn run(configuration: Configuration, api_version: &Version) -> Running

logging::setup(&log_level);

log_configuration(&configuration).await;

let configuration = Arc::new(configuration);

// Get configuration settings needed to build the app dependencies and
Expand Down Expand Up @@ -190,3 +193,9 @@ pub async fn run(configuration: Configuration, api_version: &Version) -> Running
tracker_data_importer_handle: tracker_statistics_importer_handle,
}
}

async fn log_configuration(configuration: &Configuration) {
let mut setting = configuration.get_all().await.clone();
setting.remove_secrets();
info!("Configuration:\n{}", setting.to_json());
}
6 changes: 3 additions & 3 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,17 @@ impl Info {
let env_var_config_toml_path = ENV_VAR_CONFIG_TOML_PATH.to_string();

let config_toml = if let Ok(config_toml) = env::var(env_var_config_toml) {
println!("Loading configuration from environment variable {config_toml} ...");
println!("Loading extra configuration from environment variable {config_toml} ...");
Some(config_toml)
} else {
None
};

let config_toml_path = if let Ok(config_toml_path) = env::var(env_var_config_toml_path) {
println!("Loading configuration from file: `{config_toml_path}` ...");
println!("Loading extra configuration from file: `{config_toml_path}` ...");
config_toml_path
} else {
println!("Loading configuration from default configuration file: `{default_config_toml_path}` ...");
println!("Loading extra configuration from default configuration file: `{default_config_toml_path}` ...");
default_config_toml_path
};

Expand Down
20 changes: 20 additions & 0 deletions src/config/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,26 @@ impl Settings {
"***".clone_into(&mut self.mail.smtp.credentials.password);
self.auth.secret_key = SecretKey::new("***");
}

/// Encodes the configuration to TOML.
///
/// # Panics
///
/// Will panic if it can't be converted to TOML.
#[must_use]
pub fn to_toml(&self) -> String {
toml::to_string(self).expect("Could not encode TOML value")
}

/// Encodes the configuration to JSON.
///
/// # Panics
///
/// Will panic if it can't be converted to JSON.
#[must_use]
pub fn to_json(&self) -> String {
serde_json::to_string_pretty(self).expect("Could not encode JSON value")
}
}

impl Validator for Settings {
Expand Down

0 comments on commit f39f5e5

Please sign in to comment.