From 338b3ac7c14a97a2dfefe9c9aaee0bdfc5e63e28 Mon Sep 17 00:00:00 2001 From: vikingowl Date: Wed, 6 Aug 2025 18:27:39 +0200 Subject: [PATCH] [refactor] abstracted logging initialization and app settings loading into dedicated functions to streamline `main.rs` and improve code readability --- backend-rust/src/main.rs | 42 ++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/backend-rust/src/main.rs b/backend-rust/src/main.rs index 0dac460..838c372 100644 --- a/backend-rust/src/main.rs +++ b/backend-rust/src/main.rs @@ -15,23 +15,11 @@ use tracing_subscriber::EnvFilter; #[tokio::main] async fn main() -> Result<()> { - tracing_subscriber::fmt() - .with_target(false) - .compact() - .with_env_filter(EnvFilter::from_default_env()) - .json() // For Production - .init(); + init_logging(); info!("Starting server"); - AppSettings::default(); - let mut app_settings = AppSettings::get_app_settings(); - - AppSettings::ensure_default_directory(&app_settings) - .expect("Failed to create default directory"); - - app_settings.config = ConfigFile::load_from_file(&AppSettings::get_app_settings()) - .expect("Failed to load config file"); + let app_settings = load_app_settings(); let pool = db::initialize_db(&app_settings).await?; let app = create_app(pool); @@ -65,6 +53,32 @@ async fn health_check() -> &'static str { "OK" } +fn init_logging() { + tracing_subscriber::fmt() + .with_target(false) + .compact() + // .with_env_filter(EnvFilter::from_default_env()) + // .json() // For Production + .init(); +} + +fn load_app_settings() -> AppSettings { + AppSettings::default(); + let app_settings = AppSettings::get_app_settings(); + + AppSettings::ensure_default_directory(&app_settings) + .expect("Failed to create default directory"); + + let config = ConfigFile::load_from_file(&AppSettings::get_app_settings()) + .expect("Failed to load config file"); + + let app_settings = AppSettings { + config, + ..app_settings + }; + app_settings +} + async fn shutdown_signal() { let ctrl_c = async { signal::ctrl_c()