use axum::body::Body;
use axum::extract::Request;
use axum::http::{StatusCode, header};
use axum::response::{IntoResponse, Response};
use include_dir::{Dir, include_dir};
pub static DEFAULT_SITE_DIR: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/website-default");
fn lookup(rel_path: &str) -> Option<&'static [u8]> {
let trimmed = rel_path.trim_start_matches('/');
DEFAULT_SITE_DIR.get_file(trimmed).map(|f| f.contents())
}
pub async fn serve(req: Request<Body>) -> Response {
let raw_path = req.uri().path();
let req_path = if raw_path == "/" || raw_path.ends_with('/') {
"/index.html".to_string()
} else {
raw_path.to_string()
};
let (bytes, mime) = match lookup(&req_path) {
Some(b) => (
b,
mime_guess::from_path(&req_path)
.first_or_octet_stream()
.to_string(),
),
None => match lookup("/index.html") {
Some(b) => (b, "text/html; charset=utf-8".to_string()),
None => {
return (StatusCode::NOT_FOUND, "default site not embedded").into_response();
}
},
};
Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, mime)
.header(header::CACHE_CONTROL, "public, max-age=60")
.body(Body::from(bytes))
.unwrap_or_else(|_| (StatusCode::INTERNAL_SERVER_ERROR, "response build").into_response())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn embedded_dir_has_index() {
assert!(
DEFAULT_SITE_DIR.get_file("index.html").is_some(),
"index.html missing from website-default/ — was the source dir deleted?"
);
}
#[test]
fn lookup_returns_index_bytes() {
let bytes = lookup("/index.html").expect("index.html");
let body = std::str::from_utf8(bytes).unwrap();
assert!(
body.contains("Verifiable Trust Community"),
"default index.html drifted: {body}"
);
}
#[test]
fn site_tokens_are_the_shared_tokens() {
let site = lookup("/tokens.css").expect("website-default/tokens.css");
let shared = include_bytes!("../../admin-ui/src/styles/tokens.css");
assert!(
site == shared.as_slice(),
"website-default/tokens.css has drifted from admin-ui/src/styles/tokens.css — \
edit the latter and copy it over"
);
}
#[test]
fn site_links_self_hosted_fonts_and_tokens() {
let index = std::str::from_utf8(lookup("/index.html").unwrap()).unwrap();
let fonts = index.find("href=\"/fonts.css\"").expect("fonts.css linked");
let tokens = index
.find("href=\"/tokens.css\"")
.expect("tokens.css linked");
let site = index.find("href=\"/site.css\"").expect("site.css linked");
assert!(
fonts < site && tokens < site,
"fonts and tokens precede site.css"
);
let css = std::str::from_utf8(lookup("/fonts.css").unwrap()).unwrap();
let mut named = 0;
for part in css.split("url(\"").skip(1) {
let path = &part[..part.find('"').unwrap()];
assert!(
lookup(path).is_some(),
"fonts.css names {path}, which is not embedded"
);
named += 1;
}
assert!(named >= 12, "expected the Plex faces, found {named}");
assert!(!css.contains("http"), "fonts must be same-origin");
}
#[test]
fn site_applies_the_shared_theme_before_paint() {
let index = std::str::from_utf8(lookup("/index.html").unwrap()).unwrap();
let script = index
.find("<script src=\"/theme.js\"></script>")
.expect("theme.js loaded without defer");
let head_end = index.find("</head>").expect("</head>");
assert!(script < head_end, "theme.js is loaded in <head>");
assert!(
index.contains("id=\"theme-switcher\""),
"the switcher markup"
);
let js = std::str::from_utf8(lookup("/theme.js").expect("theme.js embedded")).unwrap();
let shared = include_str!("../../admin-ui/src/lib/theme.ts");
assert!(
js.contains("\"vtc-admin-theme\"") && shared.contains("\"vtc-admin-theme\""),
"the home page and the console store the theme under one key"
);
}
#[test]
fn lookup_misses_on_unknown_path() {
assert!(lookup("/no-such-file").is_none());
}
}