use std::process::Stdio;
use valve::start;
use {argh::FromArgs, std::fmt::Debug};
#[derive(FromArgs, Debug)]
struct Cli {
#[argh(option, short = 'h', default = r#"String::from("127.0.0.1")"#)]
host: String,
#[argh(option, short = 'p', default = "3000")]
port: u16,
#[argh(option, short = 'n', default = "3")]
n_max: u16,
#[argh(option, short = 'w', default = "3")]
workers: u16,
#[argh(option, short = 'f', default = r#"String::from("plumber.R")"#)]
file: String,
#[argh(option, default = "10")]
check_unused: u32,
#[argh(option, default = "300")]
max_age: u32,
#[argh(option, default = "1")]
n_min: u16,
}
fn main() {
let cli_args: Cli = argh::from_env();
let p = std::path::Path::new(&cli_args.file).try_exists().unwrap();
if !p {
panic!("plumber file does not exist.")
}
if cli_args.n_min < 1 {
panic!("Cannot have fewer than 1 plumber API")
}
if cli_args.n_max < 1 {
panic!("Cannot have fewer than 1 plumber API")
}
if cli_args.n_min > cli_args.n_max {
panic!("`n_min` cannot be greater than `n_max`")
}
if cli_args.workers < 1 {
panic!("Cannot have fewer than 1 worker thread")
}
std::process::Command::new("R")
.arg("--version")
.stdout(Stdio::null())
.spawn()
.unwrap();
let plumber_exists = std::process::Command::new("Rscript")
.arg("-e")
.arg("library(plumber)")
.stderr(Stdio::piped())
.output()
.unwrap()
.status
.success();
if !plumber_exists {
panic!("plumber package cannot be found")
}
tokio::runtime::Builder::new_multi_thread()
.worker_threads(cli_args.workers as usize)
.enable_all()
.build()
.unwrap()
.block_on(async {
println!("Valve starting at: {}:{}", cli_args.host, cli_args.port);
start::valve_start(
cli_args.file,
cli_args.host,
cli_args.port,
cli_args.n_min.into(),
cli_args.n_max.into(),
cli_args.check_unused.try_into().unwrap(),
cli_args.max_age.try_into().unwrap(),
)
.await;
})
}