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; |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 4 | mod registry; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 5 | mod resource; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 6 | mod response; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 7 | mod run; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 8 | mod runs; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 9 | mod ui; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 10 | |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 11 | use std::sync::Arc; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 12 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 13 | use axum::{ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 14 | Router, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 15 | extract::State, |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 16 | response::{Redirect, Response}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 17 | routing::{MethodRouter, any}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 18 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 19 | |
7288fc269bb6 | atagen | 2026-10-10 13:36 UTC | 20 | use tower_http::catch_panic::CatchPanicLayer; |
7288fc269bb6 | atagen | 2026-10-10 13:36 UTC | 21 | |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 22 | use crate::{config::Config, records::Records}; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 23 | use ctx::Ctx; |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 24 | use registry::Registry; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 25 | use resource::Resource; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 26 | use run::RunResource; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 27 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 28 | pub struct AppState { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 29 | runs: Registry, |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 30 | records: Records, |
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 { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 36 | let records = match &config.db { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 37 | Some(path) => Records::open(path).unwrap_or_else(|e| { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 38 | panic!("the records at {} could not be opened: {e}", path.display()) |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 39 | }), |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 40 | None => Records::in_memory(), |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 41 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 42 | Self { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 43 | runs: Registry::default(), |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 44 | records, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 45 | config, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 46 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 47 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 48 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 49 | #[cfg(test)] |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 50 | pub(crate) fn insert(&self, run: crate::game::Run) { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 51 | self.runs.insert(run); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 52 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 53 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 54 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 55 | impl Default for AppState { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 56 | fn default() -> Self { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 57 | Self::new(Config::default()) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 58 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 59 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 60 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 61 | type Shared = Arc<AppState>; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 62 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 63 | pub fn app(config: Config) -> Router { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 64 | router(Arc::new(AppState::new(config))) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 65 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 66 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 67 | pub fn router(state: Shared) -> Router { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 68 | let api = api(state); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 69 | let ui = Router::new() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 70 | .route("/ui", any(|| async { Redirect::permanent("/ui/") })) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 71 | .route("/ui/", any(ui::page)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 72 | .route("/ui/send", any(ui::send)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 73 | .with_state(api.clone()); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 74 | api.merge(ui) |
7288fc269bb6 | atagen | 2026-10-10 13:36 UTC | 75 | .layer(CatchPanicLayer::custom(middleware::panicked)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 76 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 77 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 78 | fn global(resource: &'static Resource<Shared>) -> MethodRouter<Shared> { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 79 | any(move |State(state): State<Shared>, ctx: Ctx| async move { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 80 | offload(state, move |mut state| resource.dispatch(&mut state, &ctx)).await |
159f0998263b | atagen | 2026-10-12 11:01 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> { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 85 | any(move |State(state): State<Shared>, ctx: Ctx| async move { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 86 | offload(state, move |state| run::serve(&state, ctx, resource)).await |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 87 | }) |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 88 | } |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 89 | |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 90 | /// The model oracle blocks on HTTP for every roll, so its turns run on the blocking pool and |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 91 | /// leave the async workers free. Dice are quick enough to roll in place. |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 92 | async fn offload<F>(state: Shared, work: F) -> Response |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 93 | where |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 94 | F: FnOnce(Shared) -> Response + Send + 'static, |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 95 | { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 96 | if !state.config.oracle.blocks() { |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 97 | return work(state); |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 98 | } |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 99 | tokio::task::spawn_blocking(move || work(state)) |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 100 | .await |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 101 | .unwrap_or_else(|e| std::panic::resume_unwind(e.into_panic())) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 102 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 103 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 104 | fn api(state: Shared) -> Router { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 105 | Router::new() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 106 | .route("/", global(&meta::INDEX)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 107 | .route("/runs", global(&meta::RUNS)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 108 | .route("/runs/{id}", scoped(&runs::RUN)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 109 | .route("/runs/{id}/length", scoped(&runs::LENGTH)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 110 | .route("/runs/{id}/foreskin", scoped(&runs::FORESKIN)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 111 | .route("/runs/{id}/inventory", scoped(&runs::INVENTORY)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 112 | .route("/runs/{id}/inventory/{item}", scoped(&runs::ITEM)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 113 | .route("/runs/{id}/techniques", scoped(&runs::TECHNIQUES)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 114 | .route("/runs/{id}/drawer", scoped(&runs::DRAWER)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 115 | .route("/runs/{id}/branches/{choice}", scoped(&runs::BRANCH)) |
0a0bfee8329e | atagen | 2026-10-11 05:11 UTC | 116 | .route("/runs/{id}/kitchen", scoped(&runs::KITCHEN)) |
0a0bfee8329e | atagen | 2026-10-11 05:11 UTC | 117 | .route("/runs/{id}/kitchen/watermelon", scoped(&runs::MELON)) |
b85a0d4536c3 | atagen | 2026-10-11 08:50 UTC | 118 | .route("/runs/{id}/re-read", scoped(&runs::REREAD)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 119 | .route("/runs/{id}/floor", scoped(&runs::FLOOR)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 120 | .route("/runs/{id}/shop", scoped(&runs::SHOP)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 121 | .route("/runs/{id}/shop/{item}", scoped(&runs::BUY)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 122 | .route("/runs/{id}/portal", scoped(&runs::PORTAL)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 123 | .route("/runs/{id}/elder", scoped(&runs::ELDER)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 124 | .route("/runs/{id}/grip", scoped(&runs::GRIP)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 125 | .route("/runs/{id}/diagnosis", scoped(&runs::DIAGNOSIS)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 126 | .route("/runs/{id}/footage", scoped(&runs::FOOTAGE)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 127 | .route("/forum/threads/{year}", global(&meta::THREAD)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 128 | .route("/graveyard", global(&meta::GRAVEYARD)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 129 | .route("/leaderboard", global(&meta::LEADERBOARD)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 130 | .fallback(meta::not_found) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 131 | .layer(axum::middleware::from_fn(middleware::chrome)) |
7288fc269bb6 | atagen | 2026-10-10 13:36 UTC | 132 | .layer(CatchPanicLayer::custom(middleware::panicked)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 133 | .with_state(state) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 134 | } |