From a0ec314cacca7e713cc4441868ea6004bb1dd974 Mon Sep 17 00:00:00 2001 From: catvayor Date: Thu, 16 May 2024 22:24:47 +0200 Subject: [PATCH] event stream --- Cargo.lock | 1 + Cargo.toml | 5 +- shell.nix | 2 +- src/main.rs | 128 +++++++++++++++++++++++++++++++----- templates/conscrit.html.hbs | 9 +-- 5 files changed, 120 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e2b89f7..e2538a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1085,6 +1085,7 @@ dependencies = [ "rocket_codegen", "rocket_http", "serde", + "serde_json", "state", "tempfile", "time", diff --git a/Cargo.toml b/Cargo.toml index bdab94e..cbc8538 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,8 +5,9 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[dependencies] -rocket = "0.5.0" +[dependencies.rocket] +version = "0.5.0" +features = ["json"] [dependencies.rocket_dyn_templates] version = "0.1.0" diff --git a/shell.nix b/shell.nix index 1051055..51e7db0 100644 --- a/shell.nix +++ b/shell.nix @@ -3,5 +3,5 @@ let fenix = import (fetchTarball "https://github.com/nix-community/fenix/archive/main.tar.gz") { }; in pkgs.mkShell { - buildInputs = with fenix.latest; [ cargo rustc ]; + buildInputs = with fenix.latest; [ cargo rustc rustfmt ]; } diff --git a/src/main.rs b/src/main.rs index 1067fda..5e80c2a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,17 @@ -#[macro_use] extern crate rocket; -use rocket::fs::{FileServer, relative}; -use rocket::State; -use std::{ - collections::HashMap, - sync::RwLock, +#[macro_use] +extern crate rocket; +use rocket::{ + fs::{relative, FileServer}, + response::stream::{Event, EventStream}, + serde::Serialize, + tokio::{ + select, + time::{self, Duration}, + }, + Shutdown, State, }; -use rocket_dyn_templates::{Template, context}; +use rocket_dyn_templates::{context, Template}; +use std::{collections::HashMap, sync::RwLock}; enum TrackedState { Conscrit { @@ -19,7 +25,7 @@ enum TrackedState { Vieux { color: u8, invisible: bool, - } + }, } struct Tracked { @@ -28,20 +34,90 @@ struct Tracked { state: TrackedState, } +fn build_conscrit(name: String) -> Tracked { + Tracked { + name: name, + pos: (0.0, 0.0), + state: TrackedState::Conscrit { + invisible: false, + blurred: false, + captured: false, + malette: false, + invisibility_codes: 0, + blur_codes: 0, + }, + } +} + +fn build_vieux(name: String) -> Tracked { + Tracked { + name: name, + pos: (0.0, 0.0), + state: TrackedState::Vieux { + invisible: true, + color: 1, + }, + } +} + type Tracking = RwLock>>; -#[get("/track/conscrit")] -fn conscrit() -> Template{ - Template::render("conscrit", context!{ - name: "un nom", - id: "ID", - gpslog: "no" - }) +#[derive(Serialize)] +#[serde(crate = "rocket::serde")] +struct TrackedInfo { + name: String, + pos: (f32, f32), +} + +#[get("/track/?")] +fn tracked_view(id: &str, gpslog: Option, tracking: &State) -> Option