summaryrefslogtreecommitdiff
path: root/src/ui.rs
blob: bba01cc0db473c12b7df639b4c0fc49d9f5dfe2e (plain)
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
use axum::{
    extract::Path,
    http::{header, StatusCode},
    response::IntoResponse,
    routing::get,
    Router,
};

#[derive(rust_embed::Embed)]
#[folder = "hi-ui/build"]
struct Assets;

async fn root() -> impl IntoResponse {
    asset(Path(String::from("index.html"))).await
}

async fn asset(Path(path): Path<String>) -> impl IntoResponse {
    let mime = mime_guess::from_path(&path).first_or_octet_stream();

    match Assets::get(&path) {
        Some(file) => (
            StatusCode::OK,
            [(header::CONTENT_TYPE, mime.as_ref())],
            file.data,
        )
            .into_response(),
        None => (StatusCode::NOT_FOUND, "").into_response(),
    }
}

pub fn router<S>() -> Router<S>
where
    S: Clone + Send + Sync + 'static,
{
    Router::new()
        .route("/*path", get(asset))
        .route("/", get(root))
}