use axum::body::Body;
use axum::http::{header, StatusCode, Uri};
use axum::response::{IntoResponse, Response};
use rust_embed::Embed;
#[derive(Embed)]
#[folder = "frontend/dist/"]
struct DashboardAssets;
pub async fn serve_dashboard(uri: Uri) -> Response {
let path = uri.path().trim_start_matches('/');
let path = if path.is_empty() { "index.html" } else { path };
if let Some(file) = DashboardAssets::get(path) {
return asset_response(path, file.data);
}
if let Some(file) = DashboardAssets::get("index.html") {
return asset_response("index.html", file.data);
}
StatusCode::NOT_FOUND.into_response()
}
fn asset_response(path: &str, data: std::borrow::Cow<'static, [u8]>) -> Response {
let mime = mime_guess::from_path(path)
.first_or_octet_stream()
.to_string();
Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, mime)
.body(Body::from(data.into_owned()))
.unwrap()
}