#![warn (clippy::pedantic)] use std::{ convert::TryFrom, error::Error, path::PathBuf, sync::Arc, }; use clap::{App, SubCommand}; use tracing_subscriber::{ fmt, fmt::format::FmtSpan, EnvFilter, }; use ptth_relay::{ Config, Relay, run_relay, }; #[tokio::main] async fn main () -> Result <(), Box > { 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 (()); } let config_path = PathBuf::from ("config/ptth_relay.toml"); let config = Config::from_file (&config_path).await?; tokio::spawn (async { let webhook_url = std::env::var ("WEBHOOK_URL"); let client = reqwest::Client::default (); let mut interval = tokio::time::interval (std::time::Duration::from_secs (7200)); interval.set_missed_tick_behavior (tokio::time::MissedTickBehavior::Skip); let mut tick_seq = 0; loop { interval.tick ().await; if let Ok (webhook_url) = webhook_url.as_ref () { let now = chrono::Utc::now (); let j = serde_json::json! ({ "text": format! ("PTTH relay sent test webhook message {} at {:?}", tick_seq, now), }).to_string (); client.post (webhook_url).body (j).send ().await.ok (); tick_seq += 1; } } }); let (shutdown_rx, forced_shutdown) = ptth_core::graceful_shutdown::init_with_force (); forced_shutdown.wrap_server ( run_relay ( Arc::new (Relay::try_from (config)?), &PathBuf::new (), shutdown_rx, Some (config_path) ) ).await??; Ok (()) }