ptth/crates/ptth_relay/src/main.rs

75 lines
1.6 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::{info};
use tracing_subscriber::{
fmt,
fmt::format::FmtSpan,
EnvFilter,
};
2020-11-27 00:20:18 +00:00
use ptth_relay::{
Config,
git_version::read_git_version,
RelayState,
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-02 03:34:50 +00:00
if let Some (x) = read_git_version ().await {
info! ("ptth_relay Git version: {:?}", x);
}
else {
info! ("ptth_relay not built from Git");
}
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 (
2020-11-29 16:58:56 +00:00
Arc::new (RelayState::try_from (config)?),
Arc::new (ptth_relay::load_templates (&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 (())
}