added args for running binary
This commit is contained in:
@@ -1,48 +0,0 @@
|
||||
use config::{Config, Environment, File};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct Settings {
|
||||
pub server: ServerSettings,
|
||||
pub app: AppSettings,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct AppSettings {
|
||||
pub heartbeat_interval_secs: u64,
|
||||
pub metrics_interval_secs: u64,
|
||||
pub port: u16,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct ServerSettings {
|
||||
pub url: String,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
pub fn new() -> Result<Self, config::ConfigError> {
|
||||
dotenvy::dotenv().ok();
|
||||
|
||||
let builder = config::Config::builder()
|
||||
.add_source(File::with_name("Settings").required(false))
|
||||
.add_source(
|
||||
Environment::with_prefix("APP")
|
||||
.separator("_")
|
||||
.prefix_separator("_"),
|
||||
)
|
||||
.add_source(
|
||||
Environment::with_prefix("SERVER")
|
||||
.separator("_")
|
||||
.prefix_separator("_"),
|
||||
);
|
||||
|
||||
let config = builder.build()?;
|
||||
|
||||
// Try to deserialize and provide error message
|
||||
config.try_deserialize().map_err(|e| {
|
||||
eprintln!("Configuration error: {}", e);
|
||||
eprintln!("Required fields: server.url, app.heartbeat_interval_secs, app.metrics_interval_secs, app.port");
|
||||
e
|
||||
})
|
||||
}
|
||||
}
|
@@ -2,13 +2,11 @@
|
||||
/// 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;
|
||||
|
||||
use crate::config::Settings;
|
||||
|
||||
use std::env;
|
||||
use std::error::Error;
|
||||
use std::marker::Send;
|
||||
use std::marker::Sync;
|
||||
@@ -27,11 +25,9 @@ async fn flatten<T>(
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let settings = Settings::new().expect("Failed to load configuration");
|
||||
let args: Vec<String> = env::args().collect();
|
||||
|
||||
// Explicit variables for each .env / config field
|
||||
let _app_port = settings.app.port;
|
||||
let server_url = settings.server.url;
|
||||
let server_url = &args[1];
|
||||
|
||||
// Registration
|
||||
let (server_id, ip) = match api::register_with_server(&server_url).await {
|
||||
|
Reference in New Issue
Block a user