rename to game, add 404 page
This commit is contained in:
@@ -1,7 +1,13 @@
|
|||||||
use crate::{device_id, page::new};
|
use crate::{client_id, page::new};
|
||||||
use axum::{extract::Path, routing::get, Router};
|
use axum::{
|
||||||
|
extract::Path,
|
||||||
|
response::{IntoResponse, Response},
|
||||||
|
routing::get,
|
||||||
|
Router,
|
||||||
|
};
|
||||||
use axum_extra::extract::CookieJar;
|
use axum_extra::extract::CookieJar;
|
||||||
use maud::{html, Markup, PreEscaped};
|
use maud::{html, Markup, PreEscaped};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
fn page(content: Markup) -> Markup {
|
fn page(content: Markup) -> Markup {
|
||||||
new(html! {
|
new(html! {
|
||||||
@@ -16,18 +22,11 @@ async fn index() -> Markup {
|
|||||||
page(html! {})
|
page(html! {})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn collect(cookies: CookieJar, Path(uuid): Path<String>) -> (CookieJar, Markup) {
|
async fn game(cookies: CookieJar, Path(uuid): Path<String>) -> Response {
|
||||||
let (cookies, device) = device_id(cookies);
|
let (cookies, device) = client_id(cookies);
|
||||||
|
|
||||||
let Ok(uuid) = uuid::Uuid::parse_str(&uuid) else {
|
let Ok(uuid) = Uuid::parse_str(&uuid) else {
|
||||||
return (
|
return not_found().await.into_response();
|
||||||
cookies,
|
|
||||||
new(html! {
|
|
||||||
h1 {
|
|
||||||
"Couldn't find camera, please rescan the QR code"
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let markup = new(html! {
|
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 {
|
pub(super) fn routes() -> Router {
|
||||||
Router::new()
|
Router::new()
|
||||||
.route("/", get(index))
|
.route("/game", get(index))
|
||||||
.route("/{uuid}", get(collect))
|
.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 tower_http::services::ServeDir;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
mod collector;
|
mod game;
|
||||||
mod index;
|
mod index;
|
||||||
mod page;
|
mod page;
|
||||||
|
|
||||||
fn device_id(cookies: CookieJar) -> (CookieJar, String) {
|
fn client_id(cookies: CookieJar) -> (CookieJar, String) {
|
||||||
let mut cookies = cookies;
|
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();
|
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
|
let id = cookies
|
||||||
.get("device_id")
|
.get("client_id")
|
||||||
.expect("can't happen, as we checked above")
|
.expect("can't happen, as we checked above")
|
||||||
.to_string();
|
.to_string();
|
||||||
|
|
||||||
@@ -27,7 +27,7 @@ async fn main() {
|
|||||||
// build our application with a single route
|
// build our application with a single route
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
.route("/", get(index::index))
|
.route("/", get(index::index))
|
||||||
.nest("/cam", collector::routes())
|
.merge(game::routes())
|
||||||
.fallback_service(ServeDir::new("./static/serve"));
|
.fallback_service(ServeDir::new("./static/serve"));
|
||||||
|
|
||||||
// run our app with hyper, listening globally on port 3000
|
// run our app with hyper, listening globally on port 3000
|
||||||
|
Reference in New Issue
Block a user