Use clap for cli
This commit is contained in:
30
src/main.rs
30
src/main.rs
@ -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;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user