add session_id + uuid parsing

This commit is contained in:
2025-08-01 17:07:13 +02:00
parent 08ed2ca010
commit e46f024a75
4 changed files with 274 additions and 4 deletions

View File

@@ -1,10 +1,51 @@
use crate::page::new;
use crate::{device_id, page::new};
use axum::{extract::Path, routing::get, Router};
use axum_extra::extract::CookieJar;
use maud::{html, Markup, PreEscaped};
pub(super) async fn collector() -> Markup {
fn page(content: Markup) -> Markup {
new(html! {
hgroup {
h1 { "Digital Shadows" (PreEscaped("—")) "Who finds the most cameras?" }
(content)
}
})
}
async fn index() -> Markup {
page(html! {})
}
async fn collect(cookies: CookieJar, Path(uuid): Path<String>) -> (CookieJar, Markup) {
let (cookies, device) = device_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 markup = new(html! {
hgroup {
h1 { "Digital Shadows" (PreEscaped("&mdash;")) "Who finds the most cameras?" }
h2 {
(device)
" found camera "
(uuid)
}
}
});
(cookies, markup)
}
pub(super) fn routes() -> Router {
Router::new()
.route("/", get(index))
.route("/{uuid}", get(collect))
}

View File

@@ -1,16 +1,33 @@
use axum::{routing::get, Router};
use axum_extra::extract::{cookie::Cookie, CookieJar};
use tower_http::services::ServeDir;
use uuid::Uuid;
mod collector;
mod index;
mod page;
fn device_id(cookies: CookieJar) -> (CookieJar, String) {
let mut cookies = cookies;
if cookies.get("device_id").is_none() {
let id = Uuid::new_v4().to_string();
cookies = cookies.add(Cookie::new("device_id", id))
}
let id = cookies
.get("device_id")
.expect("can't happen, as we checked above")
.to_string();
(cookies, id)
}
#[tokio::main]
async fn main() {
// build our application with a single route
let app = Router::new()
.route("/", get(index::index))
.route("/cam", get(collector::collector))
.nest("/cam", collector::routes())
.fallback_service(ServeDir::new("./static/serve"));
// run our app with hyper, listening globally on port 3000