1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use axum::http::{StatusCode, Uri, header};
use axum::response::{IntoResponse, Response};
use rust_embed::Embed;
/// The bundle lives *inside this crate* rather than at `../did-hosting-ui/dist`
/// so it travels in the published `.crate` tarball — `cargo package` only
/// collects files under the package root, and a folder the derive cannot see is
/// a compile error. `build.rs` populates it from the `did-hosting-ui` workspace
/// when that sibling is present, and leaves the pre-bundled copy alone
/// otherwise (published crate, no Node required). See `build.rs::build_ui`.
#[derive(Embed)]
#[folder = "ui-dist"]
struct Assets;
pub async fn static_handler(uri: Uri) -> Response {
let path = uri.path().trim_start_matches('/');
// Try exact file first
if let Some(file) = Assets::get(path) {
let mime = mime_guess::from_path(path).first_or_octet_stream();
return (
StatusCode::OK,
[(header::CONTENT_TYPE, mime.as_ref())],
file.data,
)
.into_response();
}
// If path has extension, it's a genuine 404
if path.contains('.') {
return StatusCode::NOT_FOUND.into_response();
}
// Otherwise, serve index.html for client-side routing
match Assets::get("index.html") {
Some(file) => (
StatusCode::OK,
[(header::CONTENT_TYPE, "text/html")],
file.data,
)
.into_response(),
None => StatusCode::NOT_FOUND.into_response(),
}
}