//! `tabellarius-relay [--listen 127.0.0.1:8470] [--db tabellarius-relay.db] [--max-bytes N] //! [--max-mailboxes N] [--max-invites N] [--max-files N] [--max-file-bytes N] [--push-loopback]` //! //! Prints one line at start (version and address) and nothing per request. use std::sync::Arc; use std::time::{Duration, SystemTime, UNIX_EPOCH}; use tabellarius_relay::http::{router, AppState, Clock}; use tabellarius_relay::parse_args; use tabellarius_relay::store::Store; use tabellarius_relay::wake::Waker; fn now() -> i64 { SystemTime::now() .duration_since(UNIX_EPOCH) .map(|d| d.as_secs() as i64) .unwrap_or(0) } #[tokio::main] async fn main() { let args = match parse_args(std::env::args().skip(1)) { Ok(a) => a, Err(usage) => { eprintln!("{usage}"); std::process::exit(2); } }; let store = match Store::open_with(&args.db, args.limits) { Ok(s) => Arc::new(s), Err(_) => { eprintln!( "tabellarius-relay: cannot open the database {}", args.db.display() ); std::process::exit(1); } }; let listener = match tokio::net::TcpListener::bind(args.listen).await { Ok(l) => l, Err(_) => { eprintln!("tabellarius-relay: cannot listen on {}", args.listen); std::process::exit(1); } }; let bound = listener .local_addr() .map(|a| a.to_string()) .unwrap_or_default(); println!( "tabellarius-relay {} protocol {} listening on {bound}", env!("CARGO_PKG_VERSION"), tabellarius_protocol::VERSION ); // Purge at start and every hour: blobs and files after 30 days, files not complete after a // day, invites after their expiry. let purger = Arc::clone(&store); tokio::spawn(async move { let mut every_hour = tokio::time::interval(Duration::from_secs(3600)); loop { every_hour.tick().await; let _ = purger.purge(now()); } }); let clock: Clock = Arc::new(now); let app = router(AppState::with_waker( store, clock, Waker::new(args.push_loopback), )); let shutdown = async { let _ = tokio::signal::ctrl_c().await; }; if axum::serve(listener, app) .with_graceful_shutdown(shutdown) .await .is_err() { eprintln!("tabellarius-relay: the server stopped with an error"); std::process::exit(1); } }