apalis_board_api/ui/
mod.rs

1use include_dir::{Dir, File, include_dir};
2
3/// Embed the built frontend directory into the lib.
4static APP_DIST: Dir = include_dir!("$CARGO_MANIFEST_DIR/dist");
5
6#[derive(Clone, Debug, Default)]
7pub struct ServeUI;
8
9impl ServeUI {
10    pub fn new() -> Self {
11        ServeUI
12    }
13
14    /// Get an embedded file by URI path.
15    pub fn get_file(path: &str) -> Option<&File<'static>> {
16        let normalized = path.trim_start_matches('/');
17
18        APP_DIST.get_file(normalized).or_else(|| {
19            if normalized.is_empty() || !normalized.contains('.') {
20                APP_DIST.get_file("index.html")
21            } else {
22                None
23            }
24        })
25    }
26
27    /// Return a MIME type based on file extension.
28    pub fn content_type(path: &str) -> &'static str {
29        if path.ends_with(".html") {
30            "text/html; charset=utf-8"
31        } else if path.ends_with(".js") {
32            "application/javascript"
33        } else if path.ends_with(".css") {
34            "text/css"
35        } else if path.ends_with(".wasm") {
36            "application/wasm"
37        } else {
38            "application/octet-stream"
39        }
40    }
41
42    pub fn cache_control(path: &str) -> Option<&'static str> {
43        if path.ends_with(".html") {
44            // Don't cache HTML files.
45            Some("no-cache")
46        } else if path.ends_with(".js") || path.ends_with(".css") || path.ends_with(".wasm") {
47            // Cache static assets aggressively (1 year).
48            Some("public, max-age=31536000, immutable")
49        } else {
50            None
51        }
52    }
53}