Skip to main content

euv_cli/server/
struct.rs

1use crate::*;
2
3/// Shared application state.
4///
5/// Holds the generated HTML, reload channel, build lock, and CLI arguments
6/// for coordination between the HTTP server and file watcher.
7#[derive(Data, New)]
8pub struct AppState {
9    /// The generated HTML with injected reload script.
10    #[get(pub)]
11    #[get_mut(pub)]
12    #[set(pub)]
13    pub html_content: RwLock<String>,
14    /// Broadcast channel for reload events.
15    #[get(pub)]
16    #[get_mut(pub)]
17    #[set(pub)]
18    pub reload_tx: broadcast::Sender<ReloadEvent>,
19    /// Whether a build is currently in progress.
20    #[get(pub)]
21    #[get_mut(pub)]
22    #[set(pub)]
23    pub is_building: RwLock<bool>,
24    /// CLI arguments.
25    #[get(pub)]
26    #[get_mut(pub)]
27    #[set(pub)]
28    pub args: ModeArgs,
29}
30
31/// Request middleware that injects cache-control headers.
32///
33/// Sets `Cache-Control: no-cache, no-store, must-revalidate`, `Pragma: no-cache`,
34/// and `Expires: 0` on every response to prevent stale WASM assets during development.
35#[derive(Data, New)]
36pub struct RequestMiddleware;
37
38/// Response middleware that writes the serialized response to the stream.
39///
40/// Builds the HTTP response bytes and sends them through the connection stream,
41/// closing the stream if the send fails.
42#[derive(Data, New)]
43pub struct ResponseMiddleware;
44
45/// Route handler for the root path serving the injected development HTML.
46///
47/// When the request targets `index.html`, returns the in-memory HTML
48/// that has the live-reload script injected. For all other files,
49/// reads the content from disk with path-traversal protection.
50#[derive(Data, New)]
51pub struct IndexRoute;
52
53/// Route handler for the reload endpoint using long-polling.
54///
55/// Holds the connection open until a reload event is broadcast, then returns
56/// a single JSON response so the client can distinguish between a successful
57/// rebuild and an error.
58#[derive(Data, New)]
59pub struct ReloadRoute;