added .env readability
This commit is contained in:
@@ -19,6 +19,10 @@ nvml-wrapper = "0.11"
|
||||
nvml-wrapper-sys = "0.9.0"
|
||||
anyhow = "1.0.98"
|
||||
|
||||
# Docker .env loading
|
||||
config = "0.13"
|
||||
dotenvy = "0.15"
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
winapi = { version = "0.3", features = ["winuser", "pdh", "ifmib", "iphlpapi", "winerror" ,"wbemcli", "combaseapi"] }
|
||||
systemstat = "0.2.5"
|
||||
|
22
WatcherAgent/src/config.rs
Normal file
22
WatcherAgent/src/config.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
use config::Config;
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::models::WatcherConfig;
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct AppConfig {
|
||||
pub watcher: WatcherConfig,
|
||||
}
|
||||
|
||||
impl AppConfig {
|
||||
pub fn from_env() -> Result<Self, config::ConfigError> {
|
||||
// Load .env file (works in both Docker and local development)
|
||||
dotenvy::dotenv().ok();
|
||||
|
||||
let mut cfg = config::Config::builder()
|
||||
.add_source(config::Environment::with_prefix("WATCHER").separator("_"))
|
||||
.build()?;
|
||||
|
||||
cfg.try_deserialize()
|
||||
}
|
||||
}
|
@@ -2,11 +2,13 @@
|
||||
/// This agent collects hardware metrics and sends them to a backend server.
|
||||
/// It supports CPU, GPU, RAM, disk, and network metrics.
|
||||
pub mod api;
|
||||
pub mod config;
|
||||
pub mod hardware;
|
||||
pub mod metrics;
|
||||
pub mod models;
|
||||
|
||||
pub use crate::hardware::gpu;
|
||||
use config::AppConfig;
|
||||
|
||||
use std::error::Error;
|
||||
use std::marker::Send;
|
||||
use std::marker::Sync;
|
||||
@@ -25,10 +27,11 @@ async fn flatten<T>(
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let server_url = "http://localhost:5000";
|
||||
let config = AppConfig::from_env().expect("Failed to load configuration");
|
||||
let server_url = config.watcher.server_url.clone();
|
||||
|
||||
// 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 {
|
||||
Ok((id, ip)) => (id, ip),
|
||||
Err(e) => {
|
||||
eprintln!("Fehler bei der Registrierung am Server: {e}");
|
||||
|
@@ -72,3 +72,10 @@ 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,
|
||||
}
|
||||
|
Reference in New Issue
Block a user