OXYGENHUB
# e71846aa07b9 oxygenhub / crates / hub / src / web / statics.rs
2.5 KiB · Rust · last changed A trial GitHub-like look behind ?look=github (refs #74) 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: "github.css", mime: "text/css; charset=utf-8", bytes: include_bytes!("../../../../assets/github.css") },
27
    Asset { path: "hub.js", mime: "text/javascript; charset=utf-8", bytes: include_bytes!("../../../../assets/hub.js") },
28
    Asset {
29
        path: "vendor/editor.js",
30
        mime: "text/javascript; charset=utf-8",
31
        bytes: include_bytes!("../../../../assets/vendor/editor.js"),
32
    },
33
    Asset {
34
        path: "vendor/easymde.min.js",
35
        mime: "text/javascript; charset=utf-8",
36
        bytes: include_bytes!("../../../../assets/vendor/easymde.min.js"),
37
    },
38
    Asset {
39
        path: "vendor/easymde.min.css",
40
        mime: "text/css; charset=utf-8",
41
        bytes: include_bytes!("../../../../assets/vendor/easymde.min.css"),
42
    },
43
];
44
45
const CACHE: &str = "public, max-age=86400";
46
47
pub async fn style() -> Response {
48
    ([(header::CONTENT_TYPE, "text/css; charset=utf-8"), (header::CACHE_CONTROL, CACHE)], CSS).into_response()
49
}
50
51
pub async fn favicon() -> Response {
52
    ([(header::CONTENT_TYPE, "image/svg+xml"), (header::CACHE_CONTROL, CACHE)], FAVICON).into_response()
53
}
54
55
pub async fn asset(Path(path): Path<String>) -> Response {
56
    match ASSETS.iter().find(|a| a.path == path) {
57
        Some(a) => {
58
            ([(header::CONTENT_TYPE, a.mime), (header::CACHE_CONTROL, "public, max-age=31536000, immutable")], a.bytes).into_response()
59
        }
60
        None => StatusCode::NOT_FOUND.into_response(),
61
    }
62
}