use axum::{
body::Body,
http::{Response, StatusCode, header},
};
use rust_embed::Embed;
#[derive(Embed)]
#[folder = "ui/dist/"]
pub struct Assets;
pub fn serve_embedded(path: String) -> Response<Body> {
let path = if path.is_empty() || path == "/" {
"index.html".to_string()
} else {
path.trim_start_matches('/').to_string()
};
match Assets::get(&path) {
Some(content) => {
let mime = mime_guess::from_path(&path).first_or_octet_stream();
Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, mime.as_ref())
.body(Body::from(content.data.into_owned()))
.unwrap()
}
None => {
if !path.contains('.') {
if let Some(content) = Assets::get("index.html") {
return Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, "text/html")
.body(Body::from(content.data.into_owned()))
.unwrap();
}
}
Response::builder()
.status(StatusCode::NOT_FOUND)
.body(Body::from("Not Found"))
.unwrap()
}
}
}
pub fn serve_index() -> Response<Body> {
serve_embedded(String::new())
}