rename to game, add 404 page
This commit is contained in:
@@ -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<String>) -> (CookieJar, Markup) {
|
||||
let (cookies, device) = device_id(cookies);
|
||||
async fn game(cookies: CookieJar, Path(uuid): Path<String>) -> 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<String>) -> (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))
|
||||
}
|
12
src/main.rs
12
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
|
||||
|
Reference in New Issue
Block a user