use axum::{
extract::Path,
http::{self, StatusCode},
response::{Html, IntoResponse, Redirect},
};
include!(concat!(env!("OUT_DIR"), "/asset_paths.rs"));
pub async fn serve_admin_html() -> Html<&'static str> {
Html(crate::get_admin_html())
}
pub async fn serve_admin_css() -> ([(http::HeaderName, &'static str); 1], &'static str) {
([(http::header::CONTENT_TYPE, "text/css")], crate::get_admin_css())
}
pub async fn serve_admin_js() -> ([(http::HeaderName, &'static str); 1], &'static str) {
([(http::header::CONTENT_TYPE, "application/javascript")], crate::get_admin_js())
}
pub async fn serve_vendor_asset(Path(filename): Path<String>) -> impl IntoResponse {
let content_type = if filename.ends_with(".js") {
"application/javascript"
} else if filename.ends_with(".css") {
"text/css"
} else if filename.ends_with(".png") {
"image/png"
} else if filename.ends_with(".svg") {
"image/svg+xml"
} else if filename.ends_with(".woff") || filename.ends_with(".woff2") {
"font/woff2"
} else {
"application/octet-stream"
};
let asset_map = get_asset_map();
if let Some(content) = asset_map.get(filename.as_str()) {
([(http::header::CONTENT_TYPE, content_type)], *content).into_response()
} else {
(
StatusCode::NOT_FOUND,
[(http::header::CONTENT_TYPE, "text/plain")],
"Asset not found",
)
.into_response()
}
}
include!(concat!(env!("OUT_DIR"), "/icon_assets.rs"));
pub async fn serve_icon() -> impl IntoResponse {
([(http::header::CONTENT_TYPE, "image/png")], ICON_DEFAULT)
}
pub async fn serve_icon_32() -> impl IntoResponse {
([(http::header::CONTENT_TYPE, "image/png")], ICON_32)
}
pub async fn serve_icon_48() -> impl IntoResponse {
([(http::header::CONTENT_TYPE, "image/png")], ICON_48)
}
pub async fn serve_logo() -> impl IntoResponse {
([(http::header::CONTENT_TYPE, "image/png")], ICON_DEFAULT)
}
pub async fn serve_logo_40() -> impl IntoResponse {
([(http::header::CONTENT_TYPE, "image/png")], LOGO_40)
}
pub async fn serve_logo_80() -> impl IntoResponse {
([(http::header::CONTENT_TYPE, "image/png")], LOGO_80)
}
pub async fn serve_api_docs() -> impl IntoResponse {
Redirect::permanent("https://docs.mockforge.dev/api/admin-ui-rest.html")
}
pub async fn serve_manifest() -> impl IntoResponse {
(
[(http::header::CONTENT_TYPE, "application/manifest+json")],
include_str!("../../ui/public/manifest.json"),
)
}
pub async fn serve_service_worker() -> impl IntoResponse {
(
[(http::header::CONTENT_TYPE, "application/javascript")],
include_str!("../../ui/public/sw.js"),
)
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_serve_admin_html() {
let html = serve_admin_html().await;
let html_str = html.0;
assert!(!html_str.is_empty());
assert!(html_str.contains("<!DOCTYPE html>") || html_str.contains("<html"));
}
#[tokio::test]
async fn test_serve_admin_css() {
let (headers, _css) = serve_admin_css().await;
assert_eq!(headers[0].0, http::header::CONTENT_TYPE);
assert_eq!(headers[0].1, "text/css");
}
#[tokio::test]
async fn test_serve_admin_js() {
let (headers, _js) = serve_admin_js().await;
assert_eq!(headers[0].0, http::header::CONTENT_TYPE);
assert_eq!(headers[0].1, "application/javascript");
}
#[tokio::test]
async fn test_serve_icon() {
let response = serve_icon().await;
let _ = response;
}
#[tokio::test]
async fn test_serve_icon_32() {
let _ = serve_icon_32().await;
}
#[tokio::test]
async fn test_serve_icon_48() {
let _ = serve_icon_48().await;
}
#[tokio::test]
async fn test_serve_logo() {
let _ = serve_logo().await;
}
#[tokio::test]
async fn test_serve_logo_40() {
let _ = serve_logo_40().await;
}
#[tokio::test]
async fn test_serve_logo_80() {
let _ = serve_logo_80().await;
}
#[tokio::test]
async fn test_serve_api_docs() {
let _ = serve_api_docs().await;
}
}