Use clap for cli

This commit is contained in:
2024-01-31 16:55:09 +01:00
parent 38186e4371
commit f40922030c
3 changed files with 133 additions and 11 deletions

View File

@ -1,31 +1,30 @@
mod health;
mod routes;
use clap::Parser;
use dotenv::dotenv;
use health::HealthState;
use rocket::fs::FileServer;
use rocket_dyn_templates::Template;
use std::io;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use std::{env, io};
use tokio::{fs, task, time::sleep};
fn start_poller(state: Arc<HealthState>) {
task::spawn(async move {
loop {
state.update().await;
sleep(Duration::from_secs(state.config.update_period)).await;
}
});
#[derive(Parser)]
struct Opt {
/// Path to the config file.
#[clap(short, long, env = "CONFIG_PATH", default_value = "config.ron")]
config: PathBuf,
}
#[rocket::main]
async fn main() -> io::Result<()> {
dotenv().ok();
let opt = Opt::parse();
let config_path = env::var("CONFIG_PATH").unwrap_or("config.ron".into());
let config = fs::read_to_string(&config_path)
let config = fs::read_to_string(&opt.config)
.await
.expect("failed to read config file");
let config = ron::from_str(&config).expect("failed to parse config file");
@ -43,3 +42,12 @@ async fn main() -> io::Result<()> {
Ok(())
}
fn start_poller(state: Arc<HealthState>) {
task::spawn(async move {
loop {
state.update().await;
sleep(Duration::from_secs(state.config.update_period)).await;
}
});
}