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
|
|
|
};
|
2020-10-30 22:53:03 +00:00
|
|
|
|
2020-12-16 14:57:47 +00:00
|
|
|
use clap::{App, SubCommand};
|
2020-11-06 20:55:55 +00:00
|
|
|
use tracing::{info};
|
2020-11-06 23:43:52 +00:00
|
|
|
use tracing_subscriber::{
|
|
|
|
fmt,
|
|
|
|
fmt::format::FmtSpan,
|
|
|
|
EnvFilter,
|
|
|
|
};
|
2020-11-06 20:55:55 +00:00
|
|
|
|
2020-11-27 00:20:18 +00:00
|
|
|
use ptth_relay::{
|
2020-11-26 23:30:33 +00:00
|
|
|
Config,
|
2020-12-13 02:05:22 +00:00
|
|
|
git_version::read_git_version,
|
2020-11-26 23:30:33 +00:00
|
|
|
RelayState,
|
2020-11-27 00:20:18 +00:00
|
|
|
run_relay,
|
2020-11-26 23:30:33 +00:00
|
|
|
};
|
2020-11-02 18:39:19 +00:00
|
|
|
|
2020-10-30 22:53:03 +00:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main () -> Result <(), Box <dyn Error>> {
|
2020-11-06 23:43:52 +00:00
|
|
|
fmt ()
|
|
|
|
.with_env_filter (EnvFilter::from_default_env ())
|
2020-12-20 23:34:55 +00:00
|
|
|
.with_span_events (FmtSpan::CLOSE)
|
2020-11-06 23:43:52 +00:00
|
|
|
.init ()
|
|
|
|
;
|
2020-11-06 20:55:55 +00:00
|
|
|
|
2020-12-16 14:57:47 +00:00
|
|
|
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");
|
2020-11-26 23:30:33 +00:00
|
|
|
let config = Config::from_file (&config_path).await?;
|
2020-11-02 03:34:50 +00:00
|
|
|
|
2020-12-16 14:57:47 +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-12-13 02:05:22 +00:00
|
|
|
}
|
2020-11-02 18:07:34 +00:00
|
|
|
|
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)?),
|
2020-12-13 04:03:30 +00:00
|
|
|
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 (())
|
2020-10-30 22:53:03 +00:00
|
|
|
}
|