dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 1 | pub mod idle; |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 2 | |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 3 | use std::{future::Future, net::SocketAddr, time::Duration}; |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 4 | |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 5 | use axum::{ |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 6 | Router, |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 7 | http::{HeaderName, HeaderValue, StatusCode, header}, |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 8 | middleware, |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 9 | middleware::Next, |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 10 | response::{IntoResponse, Response}, |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 11 | routing::any, |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 12 | }; |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 13 | use listenfd::ListenFd; |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 14 | use tower_http::{ |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 15 | services::{ServeDir, ServeFile}, |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 16 | trace::TraceLayer, |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 17 | }; |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 18 | |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 19 | use crate::idle::IdleTracker; |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 20 | |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 21 | fn bind_addr() -> SocketAddr { |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 22 | std::env::var("MALICE_BIND") |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 23 | .unwrap_or_else(|_| "127.0.0.1:3000".to_owned()) |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 24 | .parse() |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 25 | .expect("MALICE_BIND must be host:port") |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 26 | } |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 27 | |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 28 | fn idle_exit_secs() -> Option<u64> { |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 29 | std::env::var("MALICE_IDLE_EXIT_SECS") |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 30 | .ok() |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 31 | .and_then(|value| value.trim().parse().ok()) |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 32 | } |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 33 | |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 34 | /// Takes over a systemd-activated listener from fd 3 when present, so the |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 35 | /// socket unit can hold the port before the process ever starts. Falls back |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 36 | /// to a plain bind otherwise. |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 37 | pub async fn bind_listener() -> tokio::net::TcpListener { |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 38 | let mut listenfd = ListenFd::from_env(); |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 39 | if let Some(listener) = listenfd |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 40 | .take_tcp_listener(0) |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 41 | .expect("inspecting inherited sockets") |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 42 | { |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 43 | listener |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 44 | .set_nonblocking(true) |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 45 | .expect("setting inherited socket nonblocking"); |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 46 | return tokio::net::TcpListener::from_std(listener) |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 47 | .expect("adopting inherited socket into tokio"); |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 48 | } |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 49 | let addr = bind_addr(); |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 50 | tracing::info!("listening on {addr}"); |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 51 | tokio::net::TcpListener::bind(addr) |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 52 | .await |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 53 | .expect("binding the listener must succeed") |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 54 | } |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 55 | |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 56 | /// Serves `app` on `listener`, exiting on ctrl-c or, when |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 57 | /// `MALICE_IDLE_EXIT_SECS` is set, after that long with no request in flight |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 58 | /// or started. `on_shutdown` runs after the exit signal either way. |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 59 | pub async fn serve( |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 60 | listener: tokio::net::TcpListener, |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 61 | app: Router, |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 62 | on_shutdown: impl Future<Output = ()> + Send + 'static, |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 63 | ) { |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 64 | let idle = idle_exit_secs().map(|secs| (IdleTracker::new(), secs)); |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 65 | let app = match &idle { |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 66 | Some((tracker, _)) => { |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 67 | app.layer(middleware::from_fn_with_state(tracker.clone(), idle::track)) |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 68 | } |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 69 | None => app, |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 70 | }; |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 71 | axum::serve(listener, app) |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 72 | .with_graceful_shutdown(async move { |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 73 | match idle { |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 74 | Some((tracker, secs)) => { |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 75 | tokio::select! { |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 76 | _ = tokio::signal::ctrl_c() => {} |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 77 | _ = tracker.wait_idle(Duration::from_secs(secs)) => { |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 78 | tracing::info!("idle for {secs}s with no requests, exiting"); |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 79 | } |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 80 | } |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 81 | } |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 82 | None => { |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 83 | let _ = tokio::signal::ctrl_c().await; |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 84 | } |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 85 | } |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 86 | on_shutdown.await; |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 87 | }) |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 88 | .await |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 89 | .expect("serving must succeed"); |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 90 | } |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 91 | |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 92 | async fn api_not_found() -> StatusCode { |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 93 | StatusCode::NOT_FOUND |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 94 | } |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 95 | |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 96 | async fn missing_frontend() -> Response { |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 97 | ( |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 98 | StatusCode::SERVICE_UNAVAILABLE, |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 99 | "frontend assets missing; set MALICE_WEB_DIR or use the Nix package", |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 100 | ) |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 101 | .into_response() |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 102 | } |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 103 | |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 104 | fn is_no_cache_path(path: &str) -> bool { |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 105 | matches!(path, "/" | "/index.html" | "/sw.js") |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 106 | } |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 107 | |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 108 | /// index.html and sw.js must never be cached, or a stale service worker or |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 109 | /// app shell survives a rebuild; the rest of the bundle is hashed and can be |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 110 | /// cached freely. |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 111 | async fn static_headers(request: axum::extract::Request, next: Next) -> Response { |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 112 | let no_cache = is_no_cache_path(request.uri().path()); |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 113 | let mut response = next.run(request).await; |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 114 | if no_cache { |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 115 | let headers = response.headers_mut(); |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 116 | headers.insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache")); |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 117 | headers.insert( |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 118 | HeaderName::from_static("service-worker-allowed"), |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 119 | HeaderValue::from_static("/"), |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 120 | ); |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 121 | } |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 122 | response |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 123 | } |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 124 | |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 125 | /// Serves only the built frontend: no `/api` routes, no session state, no |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 126 | /// data dir. Used for the socket-activated on-device build. |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 127 | pub fn static_router(web_dir: Option<String>) -> Router { |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 128 | let router = Router::new() |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 129 | .route("/api", any(api_not_found)) |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 130 | .route("/api/{*rest}", any(api_not_found)) |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 131 | .layer(TraceLayer::new_for_http()); |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 132 | match web_dir { |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 133 | Some(dir) => { |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 134 | let files = |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 135 | ServeDir::new(&dir).not_found_service(ServeFile::new(format!("{dir}/index.html"))); |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 136 | router |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 137 | .fallback_service(files) |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 138 | .layer(middleware::from_fn(static_headers)) |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 139 | } |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 140 | None => router.fallback(missing_frontend), |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 141 | } |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 142 | } |