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(crate) struct AppState {
9    /// The generated HTML with injected reload script.
10    #[get(pub(crate))]
11    #[get_mut(pub(crate))]
12    #[set(pub(crate))]
13    pub(crate) html_content: RwLock<String>,
14    /// Broadcast channel for reload events.
15    #[get(pub(crate))]
16    #[get_mut(pub(crate))]
17    #[set(pub(crate))]
18    pub(crate) reload_tx: broadcast::Sender<ReloadEvent>,
19    /// Whether a build is currently in progress.
20    #[get(pub(crate))]
21    #[get_mut(pub(crate))]
22    #[set(pub(crate))]
23    pub(crate) is_building: RwLock<bool>,
24    /// CLI arguments.
25    #[get(pub(crate))]
26    #[get_mut(pub(crate))]
27    #[set(pub(crate))]
28    pub(crate) args: ModeArgs,
29}
30
31/// Configuration for HTML generation.
32///
33/// Groups all parameters needed by `generate_html` into a single struct
34/// to reduce parameter count and improve maintainability.
35#[derive(Data, New)]
36pub(crate) struct HtmlConfig {
37    /// The directory where `index.html` will be written.
38    #[get(pub(crate))]
39    #[get_mut(pub(crate))]
40    #[set(pub(crate))]
41    pub(crate) serving_root: PathBuf,
42    /// The JS import path relative to the serving root.
43    #[get(pub(crate))]
44    #[get_mut(pub(crate))]
45    #[set(pub(crate))]
46    pub(crate) import_path: String,
47    /// Whether to use the release template (no live-reload).
48    #[get(pub(crate), type(copy))]
49    #[get_mut(pub(crate))]
50    #[set(pub(crate))]
51    pub(crate) is_release: bool,
52    /// Optional path to a custom index.html template file.
53    #[get(pub(crate))]
54    #[get_mut(pub(crate))]
55    #[set(pub(crate))]
56    pub(crate) custom_index_html: Option<PathBuf>,
57}
58
59/// Request middleware that injects cache-control headers.
60///
61/// Sets `Cache-Control: no-cache, no-store, must-revalidate`, `Pragma: no-cache`,
62/// and `Expires: 0` on every response to prevent stale WASM assets during development.
63#[derive(Data, New)]
64pub(crate) struct RequestMiddleware;
65
66/// Response middleware that writes the serialized response to the stream.
67///
68/// Builds the HTTP response bytes and sends them through the connection stream,
69/// closing the stream if the send fails.
70#[derive(Data, New)]
71pub(crate) struct ResponseMiddleware;
72
73/// Route handler for the root path serving the injected development HTML.
74///
75/// When the request targets `index.html`, returns the in-memory HTML
76/// that has the live-reload script injected. For all other files,
77/// reads the content from disk with path-traversal protection.
78#[derive(Data, New)]
79pub(crate) struct IndexRoute;
80
81/// Route handler for the reload endpoint using long-polling.
82///
83/// Holds the connection open until a reload event is broadcast, then returns
84/// a single JSON response so the client can distinguish between a successful
85/// rebuild and an error.
86#[derive(Data, New)]
87pub(crate) struct ReloadRoute;