use axum::{
http::{header, HeaderValue, StatusCode, Uri},
response::{IntoResponse, Response},
Router,
};
use rust_embed::RustEmbed;
pub fn spa_service<E: RustEmbed + 'static>() -> Router {
Router::new().fallback(serve::<E>)
}
async fn serve<E: RustEmbed>(uri: Uri) -> Response {
let path = uri.path().trim_start_matches('/');
E::get(path).or_else(|| E::get("index.html")).map_or_else(
|| StatusCode::NOT_FOUND.into_response(),
|file| {
let content_type = HeaderValue::from_str(file.metadata.mimetype())
.unwrap_or_else(|_| {
HeaderValue::from_static("application/octet-stream")
});
(
[
(header::CONTENT_TYPE, content_type),
(header::CACHE_CONTROL, HeaderValue::from_static("no-store")),
],
file.data.into_owned(),
)
.into_response()
},
)
}