Skip to content

Commit f39f5e5

Browse files
committed
feat: print the final configuration to console
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.
1 parent bfd3f37 commit f39f5e5

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

src/app.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::net::SocketAddr;
22
use std::sync::Arc;
33

44
use tokio::task::JoinHandle;
5+
use tracing::info;
56

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

4344
logging::setup(&log_level);
4445

46+
log_configuration(&configuration).await;
47+
4548
let configuration = Arc::new(configuration);
4649

4750
// Get configuration settings needed to build the app dependencies and
@@ -190,3 +193,9 @@ pub async fn run(configuration: Configuration, api_version: &Version) -> Running
190193
tracker_data_importer_handle: tracker_statistics_importer_handle,
191194
}
192195
}
196+
197+
async fn log_configuration(configuration: &Configuration) {
198+
let mut setting = configuration.get_all().await.clone();
199+
setting.remove_secrets();
200+
info!("Configuration:\n{}", setting.to_json());
201+
}

src/config/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,17 @@ impl Info {
141141
let env_var_config_toml_path = ENV_VAR_CONFIG_TOML_PATH.to_string();
142142

143143
let config_toml = if let Ok(config_toml) = env::var(env_var_config_toml) {
144-
println!("Loading configuration from environment variable {config_toml} ...");
144+
println!("Loading extra configuration from environment variable {config_toml} ...");
145145
Some(config_toml)
146146
} else {
147147
None
148148
};
149149

150150
let config_toml_path = if let Ok(config_toml_path) = env::var(env_var_config_toml_path) {
151-
println!("Loading configuration from file: `{config_toml_path}` ...");
151+
println!("Loading extra configuration from file: `{config_toml_path}` ...");
152152
config_toml_path
153153
} else {
154-
println!("Loading configuration from default configuration file: `{default_config_toml_path}` ...");
154+
println!("Loading extra configuration from default configuration file: `{default_config_toml_path}` ...");
155155
default_config_toml_path
156156
};
157157

src/config/v2/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,26 @@ impl Settings {
8181
"***".clone_into(&mut self.mail.smtp.credentials.password);
8282
self.auth.secret_key = SecretKey::new("***");
8383
}
84+
85+
/// Encodes the configuration to TOML.
86+
///
87+
/// # Panics
88+
///
89+
/// Will panic if it can't be converted to TOML.
90+
#[must_use]
91+
pub fn to_toml(&self) -> String {
92+
toml::to_string(self).expect("Could not encode TOML value")
93+
}
94+
95+
/// Encodes the configuration to JSON.
96+
///
97+
/// # Panics
98+
///
99+
/// Will panic if it can't be converted to JSON.
100+
#[must_use]
101+
pub fn to_json(&self) -> String {
102+
serde_json::to_string_pretty(self).expect("Could not encode JSON value")
103+
}
84104
}
85105

86106
impl Validator for Settings {

0 commit comments

Comments
 (0)