ptth/crates/ptth_relay/src/main.rs

66 lines
1.4 KiB
Rust
Raw Normal View History

2020-11-29 20:22:40 +00:00
#![warn (clippy::pedantic)]
2020-11-02 03:34:50 +00:00
use std::{
2020-11-29 16:58:56 +00:00
convert::TryFrom,
2020-11-02 03:34:50 +00:00
error::Error,
2020-11-25 03:09:21 +00:00
path::PathBuf,
2020-11-02 18:39:19 +00:00
sync::Arc,
2020-11-02 03:34:50 +00:00
};
use clap::{App, SubCommand};
use tracing_subscriber::{
fmt,
fmt::format::FmtSpan,
EnvFilter,
};
2020-11-27 00:20:18 +00:00
use ptth_relay::{
Config,
Relay,
2020-11-27 00:20:18 +00:00
run_relay,
};
2020-11-02 18:39:19 +00:00
#[tokio::main]
async fn main () -> Result <(), Box <dyn Error>> {
fmt ()
.with_env_filter (EnvFilter::from_default_env ())
.with_span_events (FmtSpan::CLOSE)
.init ()
;
let matches = App::new ("ptth_relay")
.author ("Trish")
.about ("Relay server for the PTTH backwards HTTP server")
.subcommand (SubCommand::with_name ("hash-api-key"))
.get_matches ();
if matches.subcommand_matches ("hash-api-key").is_some () {
use std::io;
use ptth_relay::key_validity::BlakeHashWrapper;
println! ("Enter key (it will be visible in the terminal)");
let mut key = String::new ();
io::stdin ().read_line (&mut key)?;
println! ("{}", BlakeHashWrapper::from_key (key.trim_end ().as_bytes ()).encode_base64 ());
return Ok (());
}
2020-11-25 03:09:21 +00:00
let config_path = PathBuf::from ("config/ptth_relay.toml");
let config = Config::from_file (&config_path).await?;
2020-11-27 00:03:11 +00:00
let (shutdown_rx, forced_shutdown) = ptth_core::graceful_shutdown::init_with_force ();
2020-11-07 02:29:45 +00:00
forced_shutdown.wrap_server (
2020-11-27 00:20:18 +00:00
run_relay (
Arc::new (Relay::try_from (config)?),
&PathBuf::new (),
2020-11-25 03:09:21 +00:00
shutdown_rx,
Some (config_path)
2020-11-07 02:29:45 +00:00
)
).await??;
Ok (())
}