use axum::{ extract::Path, http::{StatusCode, header}, response::{IntoResponse, Response}, }; use crate::StaticFiles; /// Serve embedded static files at /admin/static/*path pub async fn serve_static(Path(path): Path) -> Response { let file = match StaticFiles::get(&path) { Some(file) => file, None => return StatusCode::NOT_FOUND.into_response(), }; let mime_type = mime_from_path(&path); ( [ (header::CONTENT_TYPE, mime_type), (header::CACHE_CONTROL, cache_control_value()), ], file.data.to_vec(), ) .into_response() } fn mime_from_path(path: &str) -> &'static str { match path.rsplit('.').next() { Some("css") => "text/css; charset=utf-8", Some("js") => "application/javascript; charset=utf-8", Some("svg") => "image/svg+xml", Some("png") => "image/png", Some("jpg") | Some("jpeg") => "image/jpeg", _ => "application/octet-stream", } } fn cache_control_value() -> &'static str { if cfg!(debug_assertions) { "no-cache" } else { "public, max-age=3600, stale-while-revalidate=86400" } }