an http based roguelike rpg about jelqing
Blame: src/http/mod.rs
782069860236 | atagen | 2026-10-10 12:44 UTC | 1 | mod ctx; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 2 | mod meta; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 3 | mod middleware; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 4 | mod resource; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 5 | mod response; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 6 | mod run; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 7 | mod runs; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 8 | mod ui; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 9 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 10 | use std::{ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 11 | collections::HashMap, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 12 | sync::{Arc, Mutex, MutexGuard, PoisonError}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 13 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 14 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 15 | use axum::{ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 16 | Router, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 17 | extract::State, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 18 | response::Redirect, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 19 | routing::{MethodRouter, any}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 20 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 21 | |
7288fc269bb6 | atagen | 2026-10-10 13:36 UTC | 22 | use tower_http::catch_panic::CatchPanicLayer; |
7288fc269bb6 | atagen | 2026-10-10 13:36 UTC | 23 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 24 | use crate::{config::Config, game::Run}; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 25 | use ctx::Ctx; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 26 | use resource::Resource; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 27 | use run::RunResource; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 28 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 29 | pub struct AppState { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 30 | runs: Mutex<HashMap<String, Run>>, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 31 | config: Config, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 32 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 33 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 34 | impl AppState { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 35 | pub fn new(config: Config) -> Self { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 36 | Self { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 37 | runs: Mutex::default(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 38 | config, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 39 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 40 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 41 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 42 | /// A panic mid-request must not take every other run down with it, so a poisoned lock is recovered. |
782069860236 | atagen | 2026-10-10 12:44 UTC | 43 | fn runs(&self) -> MutexGuard<'_, HashMap<String, Run>> { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 44 | self.runs.lock().unwrap_or_else(PoisonError::into_inner) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 45 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 46 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 47 | #[cfg(test)] |
782069860236 | atagen | 2026-10-10 12:44 UTC | 48 | pub(crate) fn insert(&self, run: Run) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 49 | self.runs().insert(run.id.clone(), run); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 50 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 51 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 52 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 53 | impl Default for AppState { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 54 | fn default() -> Self { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 55 | Self::new(Config::default()) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 56 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 57 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 58 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 59 | type Shared = Arc<AppState>; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 60 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 61 | pub fn app(config: Config) -> Router { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 62 | router(Arc::new(AppState::new(config))) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 63 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 64 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 65 | pub fn router(state: Shared) -> Router { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 66 | let api = api(state); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 67 | let ui = Router::new() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 68 | .route("/ui", any(|| async { Redirect::permanent("/ui/") })) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 69 | .route("/ui/", any(ui::page)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 70 | .route("/ui/send", any(ui::send)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 71 | .with_state(api.clone()); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 72 | api.merge(ui) |
7288fc269bb6 | atagen | 2026-10-10 13:36 UTC | 73 | .layer(CatchPanicLayer::custom(middleware::panicked)) |
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 | fn global(resource: &'static Resource<Shared>) -> MethodRouter<Shared> { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 77 | any( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 78 | move |State(mut state): State<Shared>, ctx: Ctx| async move { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 79 | resource.dispatch(&mut state, &ctx) |
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 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 83 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 84 | fn scoped(resource: &'static RunResource) -> MethodRouter<Shared> { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 85 | any( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 86 | move |State(state): State<Shared>, ctx: Ctx| async move { run::serve(&state, ctx, resource) }, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 87 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 88 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 89 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 90 | fn api(state: Shared) -> Router { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 91 | Router::new() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 92 | .route("/", global(&meta::INDEX)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 93 | .route("/runs", global(&meta::RUNS)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 94 | .route("/runs/{id}", scoped(&runs::RUN)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 95 | .route("/runs/{id}/length", scoped(&runs::LENGTH)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 96 | .route("/runs/{id}/foreskin", scoped(&runs::FORESKIN)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 97 | .route("/runs/{id}/inventory", scoped(&runs::INVENTORY)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 98 | .route("/runs/{id}/inventory/{item}", scoped(&runs::ITEM)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 99 | .route("/runs/{id}/techniques", scoped(&runs::TECHNIQUES)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 100 | .route("/runs/{id}/drawer", scoped(&runs::DRAWER)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 101 | .route("/runs/{id}/branches/{choice}", scoped(&runs::BRANCH)) |
0a0bfee8329e | atagen | 2026-10-11 05:11 UTC | 102 | .route("/runs/{id}/kitchen", scoped(&runs::KITCHEN)) |
0a0bfee8329e | atagen | 2026-10-11 05:11 UTC | 103 | .route("/runs/{id}/kitchen/watermelon", scoped(&runs::MELON)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 104 | .route("/runs/{id}/floor", scoped(&runs::FLOOR)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 105 | .route("/runs/{id}/shop", scoped(&runs::SHOP)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 106 | .route("/runs/{id}/shop/{item}", scoped(&runs::BUY)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 107 | .route("/runs/{id}/portal", scoped(&runs::PORTAL)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 108 | .route("/runs/{id}/elder", scoped(&runs::ELDER)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 109 | .route("/runs/{id}/grip", scoped(&runs::GRIP)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 110 | .route("/runs/{id}/diagnosis", scoped(&runs::DIAGNOSIS)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 111 | .route("/runs/{id}/footage", scoped(&runs::FOOTAGE)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 112 | .route("/forum/threads/{year}", global(&meta::THREAD)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 113 | .route("/graveyard", global(&meta::GRAVEYARD)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 114 | .route("/leaderboard", global(&meta::LEADERBOARD)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 115 | .fallback(meta::not_found) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 116 | .layer(axum::middleware::from_fn(middleware::chrome)) |
7288fc269bb6 | atagen | 2026-10-10 13:36 UTC | 117 | .layer(CatchPanicLayer::custom(middleware::panicked)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 118 | .with_state(state) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 119 | } |