OXYGENHUB
◆ image-check oxygenhub / crates / hub / src / web / statics.rs
4.2 KiB · Rust · last changed Reports: cards with a kind icon and health, frames that fit and share the hub's look 24 days ago source blame history raw
1
//! Compiled-in static assets: the stylesheet, fonts and icons.
2
3
use axum::extract::Path;
4
use axum::http::{header, StatusCode};
5
use axum::response::{IntoResponse, Response};
6
7
const CSS: &str = include_str!("../../../../templates/style.css");
8
const FAVICON: &str = include_str!("../../../../assets/icons/oxygenhub.svg");
9
10
/// A short hash of the stylesheets and script the pages link, put in their
11
/// URLs (`?v=…`): they are served for a long time and cached by any proxy
12
/// in front of the hub (Cloudflare held a day-old stylesheet), so a new
13
/// version must have a new URL.
14
pub fn assets_version() -> &'static str {
15
    static V: std::sync::OnceLock<String> = std::sync::OnceLock::new();
16
    V.get_or_init(|| {
17
        let mut h = blake3::Hasher::new();
18
        h.update(CSS.as_bytes());
19
        for a in ASSETS.iter().filter(|a| matches!(a.path, "github.css" | "hub.js" | "report-frame.js" | "report.css")) {
20
            h.update(a.bytes);
21
        }
22
        h.finalize().to_hex()[..12].to_string()
23
    })
24
}
25
26
struct Asset {
27
    path: &'static str,
28
    mime: &'static str,
29
    bytes: &'static [u8],
30
}
31
32
const ASSETS: &[Asset] = &[
33
    Asset { path: "fonts/Barlow-300.woff2", mime: "font/woff2", bytes: include_bytes!("../../../../assets/fonts/Barlow-300.woff2") },
34
    Asset { path: "fonts/Barlow-400.woff2", mime: "font/woff2", bytes: include_bytes!("../../../../assets/fonts/Barlow-400.woff2") },
35
    Asset { path: "fonts/Barlow-500.woff2", mime: "font/woff2", bytes: include_bytes!("../../../../assets/fonts/Barlow-500.woff2") },
36
    Asset { path: "fonts/Barlow-600.woff2", mime: "font/woff2", bytes: include_bytes!("../../../../assets/fonts/Barlow-600.woff2") },
37
    Asset {
38
        path: "fonts/PressStart2P-400.woff2",
39
        mime: "font/woff2",
40
        bytes: include_bytes!("../../../../assets/fonts/PressStart2P-400.woff2"),
41
    },
42
    Asset { path: "github.css", mime: "text/css; charset=utf-8", bytes: include_bytes!("../../../../assets/github.css") },
43
    Asset { path: "hub.js", mime: "text/javascript; charset=utf-8", bytes: include_bytes!("../../../../assets/hub.js") },
44
    // For report frames (#127): the size and theme bridge the hub adds to
45
    // a report's pages, and the stylesheet a report may link.
46
    Asset { path: "report-frame.js", mime: "text/javascript; charset=utf-8", bytes: include_bytes!("../../../../assets/report-frame.js") },
47
    Asset { path: "report.css", mime: "text/css; charset=utf-8", bytes: include_bytes!("../../../../assets/report.css") },
48
    Asset {
49
        path: "vendor/editor.js",
50
        mime: "text/javascript; charset=utf-8",
51
        bytes: include_bytes!("../../../../assets/vendor/editor.js"),
52
    },
53
    Asset {
54
        path: "vendor/easymde.min.js",
55
        mime: "text/javascript; charset=utf-8",
56
        bytes: include_bytes!("../../../../assets/vendor/easymde.min.js"),
57
    },
58
    // Mermaid 12.1.0 (MIT, assets/vendor/mermaid.LICENSE), from npm's
59
    // mermaid package, dist/mermaid.min.js; loaded only by pages with a
60
    // diagram.
61
    Asset {
62
        path: "vendor/mermaid.min.js",
63
        mime: "text/javascript; charset=utf-8",
64
        bytes: include_bytes!("../../../../assets/vendor/mermaid.min.js"),
65
    },
66
    Asset {
67
        path: "vendor/easymde.min.css",
68
        mime: "text/css; charset=utf-8",
69
        bytes: include_bytes!("../../../../assets/vendor/easymde.min.css"),
70
    },
71
];
72
73
const CACHE: &str = "public, max-age=86400";
74
75
pub async fn style() -> Response {
76
    ([(header::CONTENT_TYPE, "text/css; charset=utf-8"), (header::CACHE_CONTROL, CACHE)], CSS).into_response()
77
}
78
79
pub async fn favicon() -> Response {
80
    ([(header::CONTENT_TYPE, "image/svg+xml"), (header::CACHE_CONTROL, CACHE)], FAVICON).into_response()
81
}
82
83
pub async fn asset(Path(path): Path<String>) -> Response {
84
    match ASSETS.iter().find(|a| a.path == path) {
85
        // Readable across origins: a report's sandboxed frame has an
86
        // opaque origin, and loads fonts in CORS mode (#127).
87
        Some(a) => (
88
            [
89
                (header::CONTENT_TYPE, a.mime),
90
                (header::CACHE_CONTROL, "public, max-age=31536000, immutable"),
91
                (header::ACCESS_CONTROL_ALLOW_ORIGIN, "*"),
92
            ],
93
            a.bytes,
94
        )
95
            .into_response(),
96
        None => StatusCode::NOT_FOUND.into_response(),
97
    }
98
}