| 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 | 4738× | pub fn assets_version() -> &'static str { |
| 15 | | static V: std::sync::OnceLock<String> = std::sync::OnceLock::new(); |
| 16 | 4738× | V.get_or_init(|| { |
| 17 | 121× | let mut h = blake3::Hasher::new(); |
| 18 | 121× | h.update(CSS.as_bytes()); |
| 19 | 2178× | for a in ASSETS.iter().filter(|a| { |
| 20 | 1089× | matches!( |
| 21 | 2178× | a.path, |
| 22 | 2178× | "github.css" |
| 23 | 2057× | | "hub.js" |
| 24 | 1936× | | "early.js" |
| 25 | 1815× | | "pulse.js" |
| 26 | 1694× | | "backdrop-boot.js" |
| 27 | 1573× | | "report-frame.js" |
| 28 | 1452× | | "report.css" |
| 29 | 1331× | | "backdrop/backdrop.js" |
| 30 | 1210× | | "backdrop/backdrop_bg.wasm" |
| 31 | | ) |
| 32 | 2178× | }) { |
| 33 | 1089× | h.update(a.bytes); |
| 34 | 1089× | } |
| 35 | 121× | h.finalize().to_hex()[..12].to_string() |
| 36 | 121× | }) |
| 37 | 4738× | } |
| 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 { |
| 102 | | path: "vendor/easymde.min.css", |
| 103 | | mime: "text/css; charset=utf-8", |
| 104 | | bytes: include_bytes!("../../../../assets/vendor/easymde.min.css"), |
| 105 | | }, |
| 106 | | ]; |
| 107 | |
|
| 108 | | const CACHE: &str = "public, max-age=86400"; |
| 109 | |
|
| 110 | 1× | pub async fn style() -> Response { |
| 111 | 1× | ([(header::CONTENT_TYPE, "text/css; charset=utf-8"), (header::CACHE_CONTROL, CACHE)], CSS).into_response() |
| 112 | 1× | } |
| 113 | |
|
| 114 | 0× | pub async fn favicon() -> Response { |
| 115 | 0× | ([(header::CONTENT_TYPE, "image/svg+xml"), (header::CACHE_CONTROL, CACHE)], FAVICON).into_response() |
| 116 | 0× | } |
| 117 | |
|
| 118 | 4× | pub async fn asset(Path(path): Path<String>) -> Response { |
| 119 | 35× | match ASSETS.iter().find(|a| a.path == path) { |
| 120 | | |
| 121 | | |
| 122 | 4× | Some(a) => ( |
| 123 | 4× | [ |
| 124 | 4× | (header::CONTENT_TYPE, a.mime), |
| 125 | 4× | (header::CACHE_CONTROL, "public, max-age=31536000, immutable"), |
| 126 | 4× | (header::ACCESS_CONTROL_ALLOW_ORIGIN, "*"), |
| 127 | 4× | ], |
| 128 | 4× | a.bytes, |
| 129 | 4× | ) |
| 130 | 4× | .into_response(), |
| 131 | 0× | None => StatusCode::NOT_FOUND.into_response(), |
| 132 | | } |
| 133 | 4× | } |