apalis_board_api/ui/
mod.rs1use include_dir::{Dir, File, include_dir};
2
3static 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 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 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 Some("no-cache")
46 } else if path.ends_with(".js") || path.ends_with(".css") || path.ends_with(".wasm") {
47 Some("public, max-age=31536000, immutable")
49 } else {
50 None
51 }
52 }
53}