settings devided into server and app

This commit is contained in:
2025-08-29 19:25:31 +02:00
parent 32a4435be1
commit 05afe84ffb
3 changed files with 30 additions and 24 deletions

View File

@@ -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<Self, config::ConfigError> {
// 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<Self, config::ConfigError> {
// 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()
}
}