From 9e851c310ca4381a755cc1a38f2fabbecdf21aef Mon Sep 17 00:00:00 2001 From: donpat1to Date: Mon, 11 Aug 2025 21:33:19 +0200 Subject: [PATCH] added .env readability --- .github/workflows/build.yml | 18 +----------------- WatcherAgent/Cargo.toml | 4 ++++ WatcherAgent/src/config.rs | 22 ++++++++++++++++++++++ WatcherAgent/src/main.rs | 9 ++++++--- WatcherAgent/src/models.rs | 7 +++++++ 5 files changed, 40 insertions(+), 20 deletions(-) create mode 100644 WatcherAgent/src/config.rs diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 195dd02..b86a07e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -46,10 +46,6 @@ jobs: exit 1 fi - - name: Clean up containers - run: | - docker ps -aq | xargs docker rm -f - native-build: name: Native Linux Build needs: detect-project @@ -88,10 +84,6 @@ jobs: name: linux-binary path: ${{ needs.detect-project.outputs.project-dir }}/target/x86_64-unknown-linux-gnu/release/${{ needs.detect-project.outputs.project-name }} - - name: Clean up containers - run: | - docker ps -aq | xargs docker rm -f - windows-cross: name: Windows Cross-Compile needs: detect-project @@ -129,10 +121,6 @@ jobs: name: windows-binary path: ${{ needs.detect-project.outputs.project-dir }}/target/x86_64-pc-windows-gnu/release/${{ needs.detect-project.outputs.project-name }}.exe - - name: Clean up containers - run: | - docker ps -aq | xargs docker rm -f - docker-build: name: Build Linux Docker Image needs: [native-build, windows-cross, detect-project] @@ -175,8 +163,4 @@ jobs: echo "Tagging Linux Docker image" docker tag ${{ env.IMAGE_NAME }}:linux-${{ env.TAG }} ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:linux-${{ env.TAG }} echo "Pushing Linux Docker image to registry: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:linux-${{ env.TAG }}" - docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:linux-${{ env.TAG }} - - - name: Clean up containers - run: | - docker ps -aq | xargs docker rm -f \ No newline at end of file + docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:linux-${{ env.TAG }} \ No newline at end of file diff --git a/WatcherAgent/Cargo.toml b/WatcherAgent/Cargo.toml index 35c2b05..67aea30 100644 --- a/WatcherAgent/Cargo.toml +++ b/WatcherAgent/Cargo.toml @@ -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" diff --git a/WatcherAgent/src/config.rs b/WatcherAgent/src/config.rs new file mode 100644 index 0000000..c9e3893 --- /dev/null +++ b/WatcherAgent/src/config.rs @@ -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 { + // 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() + } +} diff --git a/WatcherAgent/src/main.rs b/WatcherAgent/src/main.rs index fd1fcdc..6a68847 100644 --- a/WatcherAgent/src/main.rs +++ b/WatcherAgent/src/main.rs @@ -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( #[tokio::main] async fn main() -> Result<(), Box> { - 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}"); diff --git a/WatcherAgent/src/models.rs b/WatcherAgent/src/models.rs index 32ab76c..83a95c0 100644 --- a/WatcherAgent/src/models.rs +++ b/WatcherAgent/src/models.rs @@ -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, +}