From bedfabff9f6ba537070ae652a77e54c00c54872d Mon Sep 17 00:00:00 2001 From: Philipp Hofer Date: Sat, 2 Aug 2025 15:59:51 +0200 Subject: [PATCH] rename to game, add 404 page --- src/{collector.rs => game.rs} | 37 ++++++++++++++++++++--------------- src/main.rs | 12 ++++++------ 2 files changed, 27 insertions(+), 22 deletions(-) rename src/{collector.rs => game.rs} (53%) diff --git a/src/collector.rs b/src/game.rs similarity index 53% rename from src/collector.rs rename to src/game.rs index e073dba..974025f 100644 --- a/src/collector.rs +++ b/src/game.rs @@ -1,7 +1,13 @@ -use crate::{device_id, page::new}; -use axum::{extract::Path, routing::get, Router}; +use crate::{client_id, page::new}; +use axum::{ + extract::Path, + response::{IntoResponse, Response}, + routing::get, + Router, +}; use axum_extra::extract::CookieJar; use maud::{html, Markup, PreEscaped}; +use uuid::Uuid; fn page(content: Markup) -> Markup { new(html! { @@ -16,18 +22,11 @@ async fn index() -> Markup { page(html! {}) } -async fn collect(cookies: CookieJar, Path(uuid): Path) -> (CookieJar, Markup) { - let (cookies, device) = device_id(cookies); +async fn game(cookies: CookieJar, Path(uuid): Path) -> Response { + let (cookies, device) = client_id(cookies); - let Ok(uuid) = uuid::Uuid::parse_str(&uuid) else { - return ( - cookies, - new(html! { - h1 { - "Couldn't find camera, please rescan the QR code" - } - }), - ); + let Ok(uuid) = Uuid::parse_str(&uuid) else { + return not_found().await.into_response(); }; let markup = new(html! { @@ -41,11 +40,17 @@ async fn collect(cookies: CookieJar, Path(uuid): Path) -> (CookieJar, Ma } }); - (cookies, markup) + (cookies, markup).into_response() +} + +async fn not_found() -> Markup { + new(html! { + h1 { "uups" } + }) } pub(super) fn routes() -> Router { Router::new() - .route("/", get(index)) - .route("/{uuid}", get(collect)) + .route("/game", get(index)) + .route("/{*uuid}", get(game)) } diff --git a/src/main.rs b/src/main.rs index 7b5d85b..f863204 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,19 +3,19 @@ use axum_extra::extract::{cookie::Cookie, CookieJar}; use tower_http::services::ServeDir; use uuid::Uuid; -mod collector; +mod game; mod index; mod page; -fn device_id(cookies: CookieJar) -> (CookieJar, String) { +fn client_id(cookies: CookieJar) -> (CookieJar, String) { let mut cookies = cookies; - if cookies.get("device_id").is_none() { + if cookies.get("client_id").is_none() { let id = Uuid::new_v4().to_string(); - cookies = cookies.add(Cookie::new("device_id", id)) + cookies = cookies.add(Cookie::new("client_id", id)) } let id = cookies - .get("device_id") + .get("client_id") .expect("can't happen, as we checked above") .to_string(); @@ -27,7 +27,7 @@ async fn main() { // build our application with a single route let app = Router::new() .route("/", get(index::index)) - .nest("/cam", collector::routes()) + .merge(game::routes()) .fallback_service(ServeDir::new("./static/serve")); // run our app with hyper, listening globally on port 3000