ptth/src/bin/ptth_server.rs

49 lines
947 B
Rust

use std::{
error::Error,
path::PathBuf,
};
use structopt::StructOpt;
use tokio::sync::oneshot;
mod load_toml;
#[derive (Debug, StructOpt)]
struct Opt {
#[structopt (long)]
file_server_root: Option <PathBuf>,
}
#[tokio::main]
async fn main () -> Result <(), Box <dyn Error>> {
tracing_subscriber::fmt::init ();
let config_file = load_toml::load ("config/ptth_server.toml");
let rx = {
let (tx, rx) = oneshot::channel::<()> ();
// I have to put the tx into a Cell here so that if Ctrl-C gets
// called multiple times, we won't send multiple shutdowns to the
// oneshot channel. (Which would be a compile error)
let tx = Some (tx);
let tx = std::cell::Cell::new (tx);
ctrlc::set_handler (move ||{
let tx = tx.replace (None);
if let Some (tx) = tx {
tx.send (()).unwrap ();
}
}).expect ("Error setting Ctrl-C handler");
rx
};
ptth::server::run_server (
config_file,
rx
).await
}