an http based roguelike rpg about jelqing
Blame: src/http/mod.rs
782069860236 | atagen | 2026-10-10 12:44 UTC | 1 | mod ctx; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 2 | mod inventory; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 3 | mod leaderboard; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 4 | mod links; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 5 | mod meta; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 6 | mod middleware; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 7 | mod parse; |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 8 | mod registry; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 9 | mod resource; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 10 | mod response; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 11 | mod run; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 12 | mod runs; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 13 | mod shaft; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 14 | mod shame; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 15 | mod ui; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 16 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 17 | use std::{ |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 18 | sync::Arc, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 19 | time::{Duration, Instant}, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 20 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 21 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 22 | use axum::{ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 23 | Router, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 24 | extract::State, |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 25 | response::{Redirect, Response}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 26 | routing::{MethodRouter, any}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 27 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 28 | |
7288fc269bb6 | atagen | 2026-10-10 13:36 UTC | 29 | use tower_http::catch_panic::CatchPanicLayer; |
7288fc269bb6 | atagen | 2026-10-10 13:36 UTC | 30 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 31 | use crate::{ |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 32 | config::Config, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 33 | records::{Record, Records}, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 34 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 35 | use ctx::Ctx; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 36 | use registry::{Lock, Registry}; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 37 | use resource::Resource; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 38 | use run::RunResource; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 39 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 40 | pub struct AppState { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 41 | runs: Registry, |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 42 | records: Records, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 43 | config: Config, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 44 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 45 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 46 | impl AppState { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 47 | pub fn new(config: Config) -> rusqlite::Result<Self> { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 48 | let records = match &config.db { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 49 | Some(path) => Records::open(path)?, |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 50 | None => Records::in_memory(), |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 51 | }; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 52 | Ok(Self { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 53 | runs: Registry::default(), |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 54 | records, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 55 | config, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 56 | }) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 57 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 58 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 59 | /// Records and forgets the runs that have gone idle. The server calls it on a timer. |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 60 | pub fn sweep(&self, now: Instant) -> rusqlite::Result<()> { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 61 | for slot in self.runs.evict_idle(now, self.config.idle_ttl) { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 62 | let record = match Lock::take(&slot) { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 63 | Lock::Held(run) | Lock::Poisoned(run) => Record::of(&run), |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 64 | }; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 65 | self.records.save(&record)?; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 66 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 67 | Ok(()) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 68 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 69 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 70 | #[cfg(test)] |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 71 | pub(crate) fn insert(&self, run: crate::game::Run) { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 72 | self.runs.insert_new(run); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 73 | } |
e028a4184d00 | atagen | 2026-10-14 10:49 UTC | 74 | |
e028a4184d00 | atagen | 2026-10-14 10:49 UTC | 75 | #[cfg(test)] |
e028a4184d00 | atagen | 2026-10-14 10:49 UTC | 76 | pub(crate) fn inspect<T>(&self, visit: impl FnMut(&crate::game::Run) -> T) -> Vec<T> { |
e028a4184d00 | atagen | 2026-10-14 10:49 UTC | 77 | self.runs.each(visit) |
e028a4184d00 | atagen | 2026-10-14 10:49 UTC | 78 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 79 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 80 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 81 | impl Default for AppState { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 82 | fn default() -> Self { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 83 | Self::new(Config::default()).expect("an in-memory database always opens") |
782069860236 | atagen | 2026-10-10 12:44 UTC | 84 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 85 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 86 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 87 | type Shared = Arc<AppState>; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 88 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 89 | pub fn app(config: Config) -> rusqlite::Result<Router> { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 90 | Ok(router(Arc::new(AppState::new(config)?))) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 91 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 92 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 93 | /// Sweeps idle runs into the records every minute, so a quiet server still keeps its graveyard. |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 94 | pub async fn sweep_forever(state: Arc<AppState>) { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 95 | let mut ticks = tokio::time::interval(Duration::from_secs(60)); |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 96 | loop { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 97 | ticks.tick().await; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 98 | let swept = Arc::clone(&state); |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 99 | let result = tokio::task::spawn_blocking(move || swept.sweep(Instant::now())).await; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 100 | if let Ok(Err(error)) = result { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 101 | eprintln!("jelqd could not sweep idle runs into the records: {error}"); |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 102 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 103 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 104 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 105 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 106 | pub fn router(state: Shared) -> Router { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 107 | let api = api(state); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 108 | let ui = Router::new() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 109 | .route("/ui", any(|| async { Redirect::permanent("/ui/") })) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 110 | .route("/ui/", any(ui::page)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 111 | .route("/ui/send", any(ui::send)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 112 | .with_state(api.clone()); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 113 | api.merge(ui) |
7288fc269bb6 | atagen | 2026-10-10 13:36 UTC | 114 | .layer(CatchPanicLayer::custom(middleware::panicked)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 115 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 116 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 117 | fn global(resource: &'static Resource<Shared>) -> MethodRouter<Shared> { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 118 | any(move |State(state): State<Shared>, ctx: Ctx| async move { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 119 | offload(state, move |mut state| resource.dispatch(&mut state, &ctx)).await |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 120 | }) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 121 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 122 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 123 | fn scoped(resource: &'static RunResource) -> MethodRouter<Shared> { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 124 | any(move |State(state): State<Shared>, ctx: Ctx| async move { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 125 | offload(state, move |state| run::serve(&state, ctx, resource)).await |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 126 | }) |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 127 | } |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 128 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 129 | /// Game turns block, on SQLite always and on HTTP for every roll under the model oracle, so they |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 130 | /// run on the blocking pool and leave the async workers free. |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 131 | async fn offload<F>(state: Shared, work: F) -> Response |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 132 | where |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 133 | F: FnOnce(Shared) -> Response + Send + 'static, |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 134 | { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 135 | tokio::task::spawn_blocking(move || work(state)) |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 136 | .await |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 137 | .unwrap_or_else(|e| std::panic::resume_unwind(e.into_panic())) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 138 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 139 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 140 | fn api(state: Shared) -> Router { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 141 | Router::new() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 142 | .route("/", global(&meta::INDEX)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 143 | .route("/runs", global(&meta::RUNS)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 144 | .route("/runs/{id}", scoped(&runs::RUN)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 145 | .route("/runs/{id}/length", scoped(&runs::LENGTH)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 146 | .route("/runs/{id}/foreskin", scoped(&runs::FORESKIN)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 147 | .route("/runs/{id}/inventory", scoped(&runs::INVENTORY)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 148 | .route("/runs/{id}/inventory/{item}", scoped(&runs::ITEM)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 149 | .route("/runs/{id}/techniques", scoped(&runs::TECHNIQUES)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 150 | .route("/runs/{id}/drawer", scoped(&runs::DRAWER)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 151 | .route("/runs/{id}/branches/{choice}", scoped(&runs::BRANCH)) |
0a0bfee8329e | atagen | 2026-10-11 05:11 UTC | 152 | .route("/runs/{id}/kitchen", scoped(&runs::KITCHEN)) |
0a0bfee8329e | atagen | 2026-10-11 05:11 UTC | 153 | .route("/runs/{id}/kitchen/watermelon", scoped(&runs::MELON)) |
a11497af08aa | atagen | 2026-10-13 12:04 UTC | 154 | .route("/runs/{id}/thread", scoped(&runs::THREAD)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 155 | .route("/runs/{id}/floor", scoped(&runs::FLOOR)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 156 | .route("/runs/{id}/shop", scoped(&runs::SHOP)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 157 | .route("/runs/{id}/shop/{item}", scoped(&runs::BUY)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 158 | .route("/runs/{id}/portal", scoped(&runs::PORTAL)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 159 | .route("/runs/{id}/elder", scoped(&runs::ELDER)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 160 | .route("/runs/{id}/grip", scoped(&runs::GRIP)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 161 | .route("/runs/{id}/diagnosis", scoped(&runs::DIAGNOSIS)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 162 | .route("/runs/{id}/footage", scoped(&runs::FOOTAGE)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 163 | .route("/forum/threads/{year}", global(&meta::THREAD)) |
4f54028cba69 | atagen | 2026-10-14 11:45 UTC | 164 | .route(meta::GRAVEYARD_PATH, global(&meta::GRAVEYARD)) |
4f54028cba69 | atagen | 2026-10-14 11:45 UTC | 165 | .route(meta::LEADERBOARD_PATH, global(&meta::LEADERBOARD)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 166 | .fallback(meta::not_found) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 167 | .layer(axum::middleware::from_fn(middleware::chrome)) |
7288fc269bb6 | atagen | 2026-10-10 13:36 UTC | 168 | .layer(CatchPanicLayer::custom(middleware::panicked)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 169 | .with_state(state) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 170 | } |