[relay] F20: axum + SQLite relay storing exactly PROTOCOL.md section 5, tokens as hashes, no request log
This commit is contained in:
@@ -1,10 +1,77 @@
|
||||
//! Placeholder: prints its version and exits. Opens no socket, stores nothing.
|
||||
//! It exists so the reproducible build has an artefact to hash. The relay itself is written only
|
||||
//! after the protocol review is approved.
|
||||
//! `tabellarius-relay [--listen 127.0.0.1:8470] [--db tabellarius-relay.db]`
|
||||
//!
|
||||
//! Prints one line at start (version and address) and nothing per request.
|
||||
|
||||
fn main() {
|
||||
println!(
|
||||
"tabellarius-relay {} (no relay yet: protocol under review)",
|
||||
env!("CARGO_PKG_VERSION")
|
||||
);
|
||||
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;
|
||||
|
||||
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(&args.db) {
|
||||
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 after 30 days, 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 { store, clock });
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user