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 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 74 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 75 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 76 | impl Default for AppState { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 77 | fn default() -> Self { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 78 | Self::new(Config::default()).expect("an in-memory database always opens") |
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 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 82 | type Shared = Arc<AppState>; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 83 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 84 | pub fn app(config: Config) -> rusqlite::Result<Router> { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 85 | Ok(router(Arc::new(AppState::new(config)?))) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 86 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 87 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 88 | /// Sweeps idle runs into the records every minute, so a quiet server still keeps its graveyard. |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 89 | pub async fn sweep_forever(state: Arc<AppState>) { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 90 | let mut ticks = tokio::time::interval(Duration::from_secs(60)); |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 91 | loop { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 92 | ticks.tick().await; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 93 | let swept = Arc::clone(&state); |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 94 | let result = tokio::task::spawn_blocking(move || swept.sweep(Instant::now())).await; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 95 | if let Ok(Err(error)) = result { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 96 | eprintln!("jelqd could not sweep idle runs into the records: {error}"); |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 97 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 98 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 99 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 100 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 101 | pub fn router(state: Shared) -> Router { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 102 | let api = api(state); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 103 | let ui = Router::new() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 104 | .route("/ui", any(|| async { Redirect::permanent("/ui/") })) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 105 | .route("/ui/", any(ui::page)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 106 | .route("/ui/send", any(ui::send)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 107 | .with_state(api.clone()); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 108 | api.merge(ui) |
7288fc269bb6 | atagen | 2026-10-10 13:36 UTC | 109 | .layer(CatchPanicLayer::custom(middleware::panicked)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 110 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 111 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 112 | fn global(resource: &'static Resource<Shared>) -> MethodRouter<Shared> { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 113 | any(move |State(state): State<Shared>, ctx: Ctx| async move { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 114 | offload(state, move |mut state| resource.dispatch(&mut state, &ctx)).await |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 115 | }) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 116 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 117 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 118 | fn scoped(resource: &'static RunResource) -> MethodRouter<Shared> { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 119 | any(move |State(state): State<Shared>, ctx: Ctx| async move { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 120 | offload(state, move |state| run::serve(&state, ctx, resource)).await |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 121 | }) |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 122 | } |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 123 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 124 | /// 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 | 125 | /// run on the blocking pool and leave the async workers free. |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 126 | async fn offload<F>(state: Shared, work: F) -> Response |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 127 | where |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 128 | F: FnOnce(Shared) -> Response + Send + 'static, |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 129 | { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 130 | tokio::task::spawn_blocking(move || work(state)) |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 131 | .await |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 132 | .unwrap_or_else(|e| std::panic::resume_unwind(e.into_panic())) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 133 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 134 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 135 | fn api(state: Shared) -> Router { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 136 | Router::new() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 137 | .route("/", global(&meta::INDEX)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 138 | .route("/runs", global(&meta::RUNS)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 139 | .route("/runs/{id}", scoped(&runs::RUN)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 140 | .route("/runs/{id}/length", scoped(&runs::LENGTH)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 141 | .route("/runs/{id}/foreskin", scoped(&runs::FORESKIN)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 142 | .route("/runs/{id}/inventory", scoped(&runs::INVENTORY)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 143 | .route("/runs/{id}/inventory/{item}", scoped(&runs::ITEM)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 144 | .route("/runs/{id}/techniques", scoped(&runs::TECHNIQUES)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 145 | .route("/runs/{id}/drawer", scoped(&runs::DRAWER)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 146 | .route("/runs/{id}/branches/{choice}", scoped(&runs::BRANCH)) |
0a0bfee8329e | atagen | 2026-10-11 05:11 UTC | 147 | .route("/runs/{id}/kitchen", scoped(&runs::KITCHEN)) |
0a0bfee8329e | atagen | 2026-10-11 05:11 UTC | 148 | .route("/runs/{id}/kitchen/watermelon", scoped(&runs::MELON)) |
b85a0d4536c3 | atagen | 2026-10-11 08:50 UTC | 149 | .route("/runs/{id}/re-read", scoped(&runs::REREAD)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 150 | .route("/runs/{id}/floor", scoped(&runs::FLOOR)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 151 | .route("/runs/{id}/shop", scoped(&runs::SHOP)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 152 | .route("/runs/{id}/shop/{item}", scoped(&runs::BUY)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 153 | .route("/runs/{id}/portal", scoped(&runs::PORTAL)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 154 | .route("/runs/{id}/elder", scoped(&runs::ELDER)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 155 | .route("/runs/{id}/grip", scoped(&runs::GRIP)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 156 | .route("/runs/{id}/diagnosis", scoped(&runs::DIAGNOSIS)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 157 | .route("/runs/{id}/footage", scoped(&runs::FOOTAGE)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 158 | .route("/forum/threads/{year}", global(&meta::THREAD)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 159 | .route("/graveyard", global(&meta::GRAVEYARD)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 160 | .route("/leaderboard", global(&meta::LEADERBOARD)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 161 | .fallback(meta::not_found) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 162 | .layer(axum::middleware::from_fn(middleware::chrome)) |
7288fc269bb6 | atagen | 2026-10-10 13:36 UTC | 163 | .layer(CatchPanicLayer::custom(middleware::panicked)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 164 | .with_state(state) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 165 | } |