settings devided into server and app
This commit is contained in:
@@ -1,25 +1,35 @@
|
|||||||
use config::Config;
|
use config::{Config, Environment, File};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::models::WatcherConfig;
|
#[derive(Debug, Deserialize, Clone)]
|
||||||
|
pub struct Settings {
|
||||||
|
pub server: ServerSettings,
|
||||||
|
pub app: AppSettings,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Clone)]
|
#[derive(Debug, Deserialize, Clone)]
|
||||||
pub struct AppConfig {
|
pub struct AppSettings {
|
||||||
pub watcher: WatcherConfig,
|
pub heartbeat_interval_secs: u64,
|
||||||
|
pub metrics_interval_secs: u64,
|
||||||
|
pub port: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppConfig {
|
#[derive(Debug, Deserialize, Clone)]
|
||||||
pub fn from_env() -> Result<Self, config::ConfigError> {
|
|
||||||
// Load .env file (works in both Docker and local development)
|
pub struct ServerSettings {
|
||||||
println!("Loading .env file...");
|
pub url: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Settings {
|
||||||
|
pub fn new() -> Result<Self, config::ConfigError> {
|
||||||
|
// Load .env file first
|
||||||
dotenvy::dotenv().ok();
|
dotenvy::dotenv().ok();
|
||||||
|
|
||||||
let cfg = Config::builder()
|
// Merge settings from multiple sources
|
||||||
.add_source(config::Environment::with_prefix("WATCHER").separator("__"))
|
let builder = config::Config::builder()
|
||||||
.build()?;
|
.add_source(config::File::with_name("Settings").required(false)) // Settings.toml optional
|
||||||
|
.add_source(Environment::default().separator("_"));
|
||||||
|
|
||||||
println!("Configuration loaded: {cfg:#?}");
|
builder.build()?.try_deserialize()
|
||||||
|
|
||||||
cfg.try_deserialize()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -7,7 +7,7 @@ pub mod hardware;
|
|||||||
pub mod metrics;
|
pub mod metrics;
|
||||||
pub mod models;
|
pub mod models;
|
||||||
|
|
||||||
use crate::config::AppConfig;
|
use crate::config::Settings;
|
||||||
|
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::marker::Send;
|
use std::marker::Send;
|
||||||
@@ -27,8 +27,11 @@ async fn flatten<T>(
|
|||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
|
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||||
let config = AppConfig::from_env().expect("Failed to load configuration");
|
let settings = Settings::new().expect("Failed to load configuration");
|
||||||
let server_url = config.watcher.server_url.clone();
|
|
||||||
|
// Explicit variables for each .env / config field
|
||||||
|
let _app_port = settings.app.port;
|
||||||
|
let server_url = settings.server.url;
|
||||||
|
|
||||||
// Registration
|
// Registration
|
||||||
let (server_id, ip) = match api::register_with_server(&server_url).await {
|
let (server_id, ip) = match api::register_with_server(&server_url).await {
|
||||||
|
@@ -72,10 +72,3 @@ pub struct HardwareDto {
|
|||||||
pub ram_size: f64,
|
pub ram_size: f64,
|
||||||
pub ip_address: String,
|
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,
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user