//! Compiled-in static assets: the stylesheet, fonts and icons. use axum::extract::Path; use axum::http::{header, StatusCode}; use axum::response::{IntoResponse, Response}; const CSS: &str = include_str!("../../../../templates/style.css"); const FAVICON: &str = include_str!("../../../../assets/icons/oxygenhub.svg"); /// A short hash of the stylesheets and script the pages link, put in their /// URLs (`?v=…`): they are served for a long time and cached by any proxy /// in front of the hub (Cloudflare held a day-old stylesheet), so a new /// version must have a new URL. pub fn assets_version() -> &'static str { static V: std::sync::OnceLock = std::sync::OnceLock::new(); V.get_or_init(|| { let mut h = blake3::Hasher::new(); h.update(CSS.as_bytes()); for a in ASSETS.iter().filter(|a| a.path == "github.css" || a.path == "hub.js") { h.update(a.bytes); } h.finalize().to_hex()[..12].to_string() }) } struct Asset { path: &'static str, mime: &'static str, bytes: &'static [u8], } const ASSETS: &[Asset] = &[ Asset { path: "fonts/Barlow-300.woff2", mime: "font/woff2", bytes: include_bytes!("../../../../assets/fonts/Barlow-300.woff2") }, Asset { path: "fonts/Barlow-400.woff2", mime: "font/woff2", bytes: include_bytes!("../../../../assets/fonts/Barlow-400.woff2") }, Asset { path: "fonts/Barlow-500.woff2", mime: "font/woff2", bytes: include_bytes!("../../../../assets/fonts/Barlow-500.woff2") }, Asset { path: "fonts/Barlow-600.woff2", mime: "font/woff2", bytes: include_bytes!("../../../../assets/fonts/Barlow-600.woff2") }, Asset { path: "fonts/PressStart2P-400.woff2", mime: "font/woff2", bytes: include_bytes!("../../../../assets/fonts/PressStart2P-400.woff2"), }, Asset { path: "github.css", mime: "text/css; charset=utf-8", bytes: include_bytes!("../../../../assets/github.css") }, Asset { path: "hub.js", mime: "text/javascript; charset=utf-8", bytes: include_bytes!("../../../../assets/hub.js") }, Asset { path: "vendor/editor.js", mime: "text/javascript; charset=utf-8", bytes: include_bytes!("../../../../assets/vendor/editor.js"), }, Asset { path: "vendor/easymde.min.js", mime: "text/javascript; charset=utf-8", bytes: include_bytes!("../../../../assets/vendor/easymde.min.js"), }, Asset { path: "vendor/easymde.min.css", mime: "text/css; charset=utf-8", bytes: include_bytes!("../../../../assets/vendor/easymde.min.css"), }, ]; const CACHE: &str = "public, max-age=86400"; pub async fn style() -> Response { ([(header::CONTENT_TYPE, "text/css; charset=utf-8"), (header::CACHE_CONTROL, CACHE)], CSS).into_response() } pub async fn favicon() -> Response { ([(header::CONTENT_TYPE, "image/svg+xml"), (header::CACHE_CONTROL, CACHE)], FAVICON).into_response() } pub async fn asset(Path(path): Path) -> Response { match ASSETS.iter().find(|a| a.path == path) { Some(a) => { ([(header::CONTENT_TYPE, a.mime), (header::CACHE_CONTROL, "public, max-age=31536000, immutable")], a.bytes).into_response() } None => StatusCode::NOT_FOUND.into_response(), } }