chore(api): update dist.rs

This commit is contained in:
ZhenYi 2026-04-20 15:45:30 +08:00
parent 0e4631ec75
commit 26865f8dcf

View File

@ -1,29 +1,69 @@
use actix_web::{web, HttpResponse}; use actix_web::{http::header, web, HttpRequest, HttpResponse};
use mime_guess2::MimeGuess; use mime_guess2::MimeGuess;
pub async fn serve_frontend(path: web::Path<String>) -> HttpResponse { fn cache_control_header(path: &str) -> &'static str {
let path = path.into_inner(); if path == "index.html" {
let path = if path.is_empty() || path == "/" { "no-cache, no-store, must-revalidate"
"index.html" } else if path.ends_with(".js")
|| path.ends_with(".css")
|| path.ends_with(".woff2")
|| path.ends_with(".woff")
|| path.ends_with(".ttf")
|| path.ends_with(".otf")
|| path.ends_with(".png")
|| path.ends_with(".jpg")
|| path.ends_with(".jpeg")
|| path.ends_with(".gif")
|| path.ends_with(".svg")
|| path.ends_with(".ico")
|| path.ends_with(".webp")
|| path.ends_with(".avif")
|| path.ends_with(".map")
{
"public, max-age=31536000, immutable"
} else { } else {
&path "no-cache"
}; }
}
match frontend::get_frontend_asset(path) {
Some(data) => { pub async fn serve_frontend(req: HttpRequest, path: web::Path<String>) -> HttpResponse {
let mime = MimeGuess::from_path(path).first_or_octet_stream(); let path = path.into_inner();
HttpResponse::Ok() let path_str = if path.is_empty() || path == "/" {
.content_type(mime.as_ref()) "index.html"
.body(data.to_vec()) } else {
} path.as_str()
None => { };
// Fallback to index.html for SPA routing
match frontend::get_frontend_asset("index.html") { let cc = cache_control_header(path_str);
Some(data) => HttpResponse::Ok()
.content_type("text/html") match frontend::get_frontend_asset_with_etag(path_str) {
.body(data.to_vec()), Some((data, etag)) => {
None => HttpResponse::NotFound().finish(), // Check If-None-Match for conditional request
} if let Some(if_none_match) = req.headers().get(header::IF_NONE_MATCH) {
} if let Ok(client_etag) = if_none_match.to_str() {
if client_etag == etag {
return HttpResponse::NotModified()
.insert_header(("Cache-Control", cc))
.insert_header(("ETag", etag))
.finish();
}
}
}
let mime = MimeGuess::from_path(path_str).first_or_octet_stream();
HttpResponse::Ok()
.content_type(mime.as_ref())
.insert_header(("Cache-Control", cc))
.insert_header(("ETag", etag))
.body(data.to_vec())
}
None => match frontend::get_frontend_asset_with_etag("index.html") {
Some((data, etag)) => HttpResponse::Ok()
.content_type("text/html")
.insert_header(("Cache-Control", "no-cache, no-store, must-revalidate"))
.insert_header(("ETag", etag))
.body(data.to_vec()),
None => HttpResponse::NotFound().finish(),
},
} }
} }