From a00df617c52efb9c6c31b602b164c54c1b0e8082 Mon Sep 17 00:00:00 2001 From: catvayor Date: Mon, 10 Jun 2024 09:43:35 +0200 Subject: [PATCH] feat(admin): admin page --- src/main.rs | 151 +++++++++++++++++++----- static/utils.js | 3 + templates/admin.html | 166 --------------------------- templates/admin.html.hbs | 221 ++++++++++++++++++++++++++++++++++++ templates/conscrit.html.hbs | 1 + templates/vieux.html.hbs | 1 + 6 files changed, 346 insertions(+), 197 deletions(-) delete mode 100644 templates/admin.html create mode 100644 templates/admin.html.hbs diff --git a/src/main.rs b/src/main.rs index 06ea57d..7a56a54 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use rand::Rng; use rocket::{ fs::{relative, FileServer}, response::stream::{Event, EventStream}, - serde::Serialize, + serde::{json::Json, Deserialize, Serialize}, tokio::{ self, select, time::{self, Duration, Instant}, @@ -17,7 +17,7 @@ use std::{ sync::{Arc, RwLock}, }; -#[derive(Serialize, Clone)] +#[derive(Serialize, Deserialize, Clone)] #[serde(crate = "rocket::serde")] enum TrackedState { Conscrit { @@ -161,6 +161,8 @@ impl From for Event { type Tracking = Arc>>>; type TrackingEventQueue = Arc>>>>; +type AdminEventQueue = Arc>>; +type AdminKey = String; #[derive(Serialize)] #[serde(crate = "rocket::serde")] @@ -268,47 +270,33 @@ fn tracked_view( } } -fn evts_to_send(id: &str, evt_queue: &TrackingEventQueue) -> Vec { - let mut evts = Vec::new(); - for qevt in evt_queue - .read() - .unwrap() - .get(&id.to_string()) - .unwrap() +fn evts_to_send(evt_queue: &RwLock>) -> Vec { + evt_queue .read() .unwrap() .iter() .filter(|qevt| !qevt.expired()) - { - evts.push(qevt.evt.clone()); - } - evts + .map(|qevt| qevt.evt.clone()) + .collect() +} + +fn evts_for(id: &str, evt_queues: &TrackingEventQueue) -> Vec { + evts_to_send(evt_queues.read().unwrap().get(&id.to_string()).unwrap()) } #[get("/track//events")] fn tracked_events<'a>( id: &'a str, - tracking: &State, evt_queue: &'a State, mut shutdown: Shutdown, ) -> Option { if evt_queue.read().unwrap().contains_key(&id.to_string()) { - state_update( - &tracking - .read() - .unwrap() - .get(&id.to_string()) - .unwrap() - .read() - .unwrap(), - &evt_queue, - ); Some(EventStream! { let mut interval = time::interval(Duration::from_millis(100)); loop { select!{ _ = interval.tick() =>{ - for evt in evts_to_send(id, evt_queue){ + for evt in evts_for(id, evt_queue){ //println!("{:?}", evt); yield evt; } @@ -322,7 +310,79 @@ fn tracked_events<'a>( } } -fn state_update(tracked: &Tracked, evt_queues: &TrackingEventQueue) { +#[get("/admin?&")] +fn admin_page( + tok: Option, + dbg: Option, + admin_key: &State, +) -> Option