From 05afe84ffb2d24d19cda07b13f8f87fb066aea1a Mon Sep 17 00:00:00 2001 From: donpat1to Date: Fri, 29 Aug 2025 19:25:31 +0200 Subject: [PATCH] settings devided into server and app --- WatcherAgent/src/config.rs | 38 ++++++++++++++++++++++++-------------- WatcherAgent/src/main.rs | 9 ++++++--- WatcherAgent/src/models.rs | 7 ------- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/WatcherAgent/src/config.rs b/WatcherAgent/src/config.rs index eaf1f87..dd9fd17 100644 --- a/WatcherAgent/src/config.rs +++ b/WatcherAgent/src/config.rs @@ -1,25 +1,35 @@ -use config::Config; +use config::{Config, Environment, File}; use serde::Deserialize; -use crate::models::WatcherConfig; +#[derive(Debug, Deserialize, Clone)] +pub struct Settings { + pub server: ServerSettings, + pub app: AppSettings, +} #[derive(Debug, Deserialize, Clone)] -pub struct AppConfig { - pub watcher: WatcherConfig, +pub struct AppSettings { + pub heartbeat_interval_secs: u64, + pub metrics_interval_secs: u64, + pub port: u16, } -impl AppConfig { - pub fn from_env() -> Result { - // Load .env file (works in both Docker and local development) - println!("Loading .env file..."); +#[derive(Debug, Deserialize, Clone)] + +pub struct ServerSettings { + pub url: String, +} + +impl Settings { + pub fn new() -> Result { + // Load .env file first dotenvy::dotenv().ok(); - let cfg = Config::builder() - .add_source(config::Environment::with_prefix("WATCHER").separator("__")) - .build()?; + // Merge settings from multiple sources + let builder = config::Config::builder() + .add_source(config::File::with_name("Settings").required(false)) // Settings.toml optional + .add_source(Environment::default().separator("_")); - println!("Configuration loaded: {cfg:#?}"); - - cfg.try_deserialize() + builder.build()?.try_deserialize() } } diff --git a/WatcherAgent/src/main.rs b/WatcherAgent/src/main.rs index a75503b..d371b56 100644 --- a/WatcherAgent/src/main.rs +++ b/WatcherAgent/src/main.rs @@ -7,7 +7,7 @@ pub mod hardware; pub mod metrics; pub mod models; -use crate::config::AppConfig; +use crate::config::Settings; use std::error::Error; use std::marker::Send; @@ -27,8 +27,11 @@ async fn flatten( #[tokio::main] async fn main() -> Result<(), Box> { - let config = AppConfig::from_env().expect("Failed to load configuration"); - let server_url = config.watcher.server_url.clone(); + let settings = Settings::new().expect("Failed to load configuration"); + + // Explicit variables for each .env / config field + let _app_port = settings.app.port; + let server_url = settings.server.url; // Registration let (server_id, ip) = match api::register_with_server(&server_url).await { diff --git a/WatcherAgent/src/models.rs b/WatcherAgent/src/models.rs index 83a95c0..32ab76c 100644 --- a/WatcherAgent/src/models.rs +++ b/WatcherAgent/src/models.rs @@ -72,10 +72,3 @@ pub struct HardwareDto { pub ram_size: f64, pub ip_address: String, } - -#[derive(Debug, Deserialize, Clone)] -pub struct WatcherConfig { - pub server_url: String, - pub heartbeat_interval_secs: u64, - pub metrics_interval_secs: u64, -}