OXYGENHUB
# 3f02e5ea4f2d oxygenhub / crates / hub / src / web / statics.rs
1.9 KiB · Rust · last changed Milestone 0.1: browse 1 month 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
struct Asset {
11
    path: &'static str,
12
    mime: &'static str,
13
    bytes: &'static [u8],
14
}
15
16
const ASSETS: &[Asset] = &[
17
    Asset { path: "fonts/Barlow-300.woff2", mime: "font/woff2", bytes: include_bytes!("../../../../assets/fonts/Barlow-300.woff2") },
18
    Asset { path: "fonts/Barlow-400.woff2", mime: "font/woff2", bytes: include_bytes!("../../../../assets/fonts/Barlow-400.woff2") },
19
    Asset { path: "fonts/Barlow-500.woff2", mime: "font/woff2", bytes: include_bytes!("../../../../assets/fonts/Barlow-500.woff2") },
20
    Asset { path: "fonts/Barlow-600.woff2", mime: "font/woff2", bytes: include_bytes!("../../../../assets/fonts/Barlow-600.woff2") },
21
    Asset {
22
        path: "fonts/PressStart2P-400.woff2",
23
        mime: "font/woff2",
24
        bytes: include_bytes!("../../../../assets/fonts/PressStart2P-400.woff2"),
25
    },
26
    Asset { path: "hub.js", mime: "text/javascript; charset=utf-8", bytes: include_bytes!("../../../../assets/hub.js") },
27
];
28
29
const CACHE: &str = "public, max-age=86400";
30
31
pub async fn style() -> Response {
32
    ([(header::CONTENT_TYPE, "text/css; charset=utf-8"), (header::CACHE_CONTROL, CACHE)], CSS).into_response()
33
}
34
35
pub async fn favicon() -> Response {
36
    ([(header::CONTENT_TYPE, "image/svg+xml"), (header::CACHE_CONTROL, CACHE)], FAVICON).into_response()
37
}
38
39
pub async fn asset(Path(path): Path<String>) -> Response {
40
    match ASSETS.iter().find(|a| a.path == path) {
41
        Some(a) => {
42
            ([(header::CONTENT_TYPE, a.mime), (header::CACHE_CONTROL, "public, max-age=31536000, immutable")], a.bytes).into_response()
43
        }
44
        None => StatusCode::NOT_FOUND.into_response(),
45
    }
46
}