added .env readability

This commit is contained in:
2025-08-11 21:33:19 +02:00
parent df36cb53c7
commit 9e851c310c
5 changed files with 40 additions and 20 deletions

View File

@@ -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
docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:linux-${{ env.TAG }}

View File

@@ -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"

View 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()
}
}

View File

@@ -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}");

View File

@@ -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,
}