| 1 | |
|
| 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 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | 5019× | pub fn assets_version() -> &'static str { |
| 15 | | static V: std::sync::OnceLock<String> = std::sync::OnceLock::new(); |
| 16 | 5019× | V.get_or_init(|| { |
| 17 | 129× | let mut h = blake3::Hasher::new(); |
| 18 | 129× | h.update(CSS.as_bytes()); |
| 19 | 2580× | for a in ASSETS.iter().filter(|a| { |
| 20 | 1161× | matches!( |
| 21 | 2580× | a.path, |
| 22 | 2580× | "github.css" |
| 23 | 2451× | | "hub.js" |
| 24 | 2322× | | "early.js" |
| 25 | 2193× | | "pulse.js" |
| 26 | 2064× | | "backdrop-boot.js" |
| 27 | 1935× | | "report-frame.js" |
| 28 | 1806× | | "report.css" |
| 29 | 1677× | | "backdrop/backdrop.js" |
| 30 | 1548× | | "backdrop/backdrop_bg.wasm" |
| 31 | | ) |
| 32 | 2580× | }) { |
| 33 | 1161× | h.update(a.bytes); |
| 34 | 1161× | } |
| 35 | 129× | h.finalize().to_hex()[..12].to_string() |
| 36 | 129× | }) |
| 37 | 5019× | } |
| 38 | |
|
| 39 | | struct Asset { |
| 40 | | path: &'static str, |
| 41 | | mime: &'static str, |
| 42 | | bytes: &'static [u8], |
| 43 | | } |
| 44 | |
|
| 45 | | const ASSETS: &[Asset] = &[ |
| 46 | | Asset { path: "fonts/Barlow-300.woff2", mime: "font/woff2", bytes: include_bytes!("../../../../assets/fonts/Barlow-300.woff2") }, |
| 47 | | Asset { path: "fonts/Barlow-400.woff2", mime: "font/woff2", bytes: include_bytes!("../../../../assets/fonts/Barlow-400.woff2") }, |
| 48 | | Asset { path: "fonts/Barlow-500.woff2", mime: "font/woff2", bytes: include_bytes!("../../../../assets/fonts/Barlow-500.woff2") }, |
| 49 | | Asset { path: "fonts/Barlow-600.woff2", mime: "font/woff2", bytes: include_bytes!("../../../../assets/fonts/Barlow-600.woff2") }, |
| 50 | | Asset { |
| 51 | | path: "fonts/PressStart2P-400.woff2", |
| 52 | | mime: "font/woff2", |
| 53 | | bytes: include_bytes!("../../../../assets/fonts/PressStart2P-400.woff2"), |
| 54 | | }, |
| 55 | | Asset { path: "github.css", mime: "text/css; charset=utf-8", bytes: include_bytes!("../../../../assets/github.css") }, |
| 56 | | Asset { path: "hub.js", mime: "text/javascript; charset=utf-8", bytes: include_bytes!("../../../../assets/hub.js") }, |
| 57 | | |
| 58 | | |
| 59 | | Asset { path: "early.js", mime: "text/javascript; charset=utf-8", bytes: include_bytes!("../../../../assets/early.js") }, |
| 60 | | Asset { path: "pulse.js", mime: "text/javascript; charset=utf-8", bytes: include_bytes!("../../../../assets/pulse.js") }, |
| 61 | | Asset { |
| 62 | | path: "backdrop-boot.js", |
| 63 | | mime: "text/javascript; charset=utf-8", |
| 64 | | bytes: include_bytes!("../../../../assets/backdrop-boot.js"), |
| 65 | | }, |
| 66 | | |
| 67 | | |
| 68 | | Asset { path: "report-frame.js", mime: "text/javascript; charset=utf-8", bytes: include_bytes!("../../../../assets/report-frame.js") }, |
| 69 | | Asset { path: "report.css", mime: "text/css; charset=utf-8", bytes: include_bytes!("../../../../assets/report.css") }, |
| 70 | | |
| 71 | | |
| 72 | | |
| 73 | | Asset { |
| 74 | | path: "backdrop/backdrop.js", |
| 75 | | mime: "text/javascript; charset=utf-8", |
| 76 | | bytes: include_bytes!("../../../../assets/backdrop/backdrop.js"), |
| 77 | | }, |
| 78 | | Asset { |
| 79 | | path: "backdrop/backdrop_bg.wasm", |
| 80 | | mime: "application/wasm", |
| 81 | | bytes: include_bytes!("../../../../assets/backdrop/backdrop_bg.wasm"), |
| 82 | | }, |
| 83 | | Asset { |
| 84 | | path: "vendor/editor.js", |
| 85 | | mime: "text/javascript; charset=utf-8", |
| 86 | | bytes: include_bytes!("../../../../assets/vendor/editor.js"), |
| 87 | | }, |
| 88 | | Asset { |
| 89 | | path: "vendor/easymde.min.js", |
| 90 | | mime: "text/javascript; charset=utf-8", |
| 91 | | bytes: include_bytes!("../../../../assets/vendor/easymde.min.js"), |
| 92 | | }, |
| 93 | | |
| 94 | | |
| 95 | | |
| 96 | | Asset { |
| 97 | | path: "vendor/mermaid.min.js", |
| 98 | | mime: "text/javascript; charset=utf-8", |
| 99 | | bytes: include_bytes!("../../../../assets/vendor/mermaid.min.js"), |
| 100 | | }, |
| 101 | | Asset { path: "wasi-worker.js", mime: "text/javascript; charset=utf-8", bytes: include_bytes!("../../../../assets/wasi-worker.js") }, |
| 102 | | Asset { path: "wasm-tab.js", mime: "text/javascript; charset=utf-8", bytes: include_bytes!("../../../../assets/wasm-tab.js") }, |
| 103 | | Asset { |
| 104 | | path: "vendor/easymde.min.css", |
| 105 | | mime: "text/css; charset=utf-8", |
| 106 | | bytes: include_bytes!("../../../../assets/vendor/easymde.min.css"), |
| 107 | | }, |
| 108 | | ]; |
| 109 | |
|
| 110 | | const CACHE: &str = "public, max-age=86400"; |
| 111 | |
|
| 112 | 1× | pub async fn style() -> Response { |
| 113 | 1× | ([(header::CONTENT_TYPE, "text/css; charset=utf-8"), (header::CACHE_CONTROL, CACHE)], CSS).into_response() |
| 114 | 1× | } |
| 115 | |
|
| 116 | 0× | pub async fn favicon() -> Response { |
| 117 | 0× | ([(header::CONTENT_TYPE, "image/svg+xml"), (header::CACHE_CONTROL, CACHE)], FAVICON).into_response() |
| 118 | 0× | } |
| 119 | |
|
| 120 | 5× | pub async fn asset(Path(path): Path<String>) -> Response { |
| 121 | 53× | match ASSETS.iter().find(|a| a.path == path) { |
| 122 | | |
| 123 | | |
| 124 | 5× | Some(a) => ( |
| 125 | 5× | [ |
| 126 | 5× | (header::CONTENT_TYPE, a.mime), |
| 127 | 5× | (header::CACHE_CONTROL, "public, max-age=31536000, immutable"), |
| 128 | 5× | (header::ACCESS_CONTROL_ALLOW_ORIGIN, "*"), |
| 129 | 5× | ], |
| 130 | 5× | a.bytes, |
| 131 | 5× | ) |
| 132 | 5× | .into_response(), |
| 133 | 0× | None => StatusCode::NOT_FOUND.into_response(), |
| 134 | | } |
| 135 | 5× | } |