use std::env;
use std::path::Path;
use std::process;
use argh::FromArgs;
use qrsync::{QrSyncHttp, QrSyncResult};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
#[derive(FromArgs, Debug)]
struct Opts {
#[argh(positional)]
filename: Option<String>,
#[argh(option, short = 'r')]
root_dir: Option<String>,
#[argh(switch, short = 'd')]
debug: bool,
#[argh(option, short = 'p', default = "5566")]
port: u16,
#[argh(option, short = 'i')]
ip_address: Option<String>,
#[argh(switch, short = 'l')]
light_term: bool,
#[argh(switch, short = '6')]
ipv6: bool,
#[argh(switch, short = 'v')]
version: bool,
}
fn setup_tracing(debug: bool) {
let level = if debug { "debug" } else { "info" };
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| format!("qrsync={level},tower_http={level},axum::rejection=trace").into()),
)
.with(tracing_subscriber::fmt::layer())
.init();
}
fn register_signal_handlers() -> QrSyncResult<()> {
ctrlc::set_handler(move || {
tracing::warn!("Shutting down QrSync server");
process::exit(0);
})?;
Ok(())
}
async fn run() -> QrSyncResult<()> {
let opts: Opts = argh::from_env();
if opts.version {
println!("qrsync v{} - {}", env!("CARGO_PKG_VERSION"), env!("CARGO_PKG_AUTHORS"));
process::exit(0)
}
setup_tracing(opts.debug);
tracing::debug!("Command line options are {:#?}", opts);
register_signal_handlers()?;
let root_dir = match opts.root_dir {
Some(r) => Path::new(&r).to_path_buf(),
None => env::current_dir()?,
};
let http = QrSyncHttp::new(
opts.ip_address,
opts.port,
opts.filename,
root_dir,
opts.light_term,
opts.ipv6,
);
http.run().await?;
Ok(())
}
#[tokio::main]
async fn main() -> ! {
match run().await {
Ok(_) => {
tracing::info!("QrSync run successfully");
process::exit(0);
}
Err(e) => {
tracing::error!("Error running QrSync: {}", e);
process::exit(1);
}
}
}