adk_studio/
embedded.rs

1use axum::{
2    body::Body,
3    http::{Response, StatusCode, header},
4};
5use rust_embed::Embed;
6
7#[derive(Embed)]
8#[folder = "ui/dist/"]
9pub struct Assets;
10
11/// Serve embedded static files
12pub fn serve_embedded(path: String) -> Response<Body> {
13    let path = if path.is_empty() || path == "/" {
14        "index.html".to_string()
15    } else {
16        path.trim_start_matches('/').to_string()
17    };
18
19    match Assets::get(&path) {
20        Some(content) => {
21            let mime = mime_guess::from_path(&path).first_or_octet_stream();
22            Response::builder()
23                .status(StatusCode::OK)
24                .header(header::CONTENT_TYPE, mime.as_ref())
25                .body(Body::from(content.data.into_owned()))
26                .unwrap()
27        }
28        None => {
29            // For SPA routing, serve index.html for non-file paths
30            if !path.contains('.') {
31                if let Some(content) = Assets::get("index.html") {
32                    return Response::builder()
33                        .status(StatusCode::OK)
34                        .header(header::CONTENT_TYPE, "text/html")
35                        .body(Body::from(content.data.into_owned()))
36                        .unwrap();
37                }
38            }
39            Response::builder().status(StatusCode::NOT_FOUND).body(Body::from("Not Found")).unwrap()
40        }
41    }
42}
43
44/// Serve the index.html for root path
45pub fn serve_index() -> Response<Body> {
46    serve_embedded(String::new())
47}