diff options
| author | Owen Jacobson <owen@grimoire.ca> | 2024-09-28 00:38:42 -0400 |
|---|---|---|
| committer | Owen Jacobson <owen@grimoire.ca> | 2024-10-05 18:16:26 -0400 |
| commit | ec51666002900faa389ff0c8f34ed32c663bd723 (patch) | |
| tree | d3de7810df562128ddacac1dcb57b199acdfe2a3 /src/ui.rs | |
| parent | 671b36c2c8d4026eb7a34f0ac7bd5720b34f1a0d (diff) | |
Render the UI at /.
Diffstat (limited to 'src/ui.rs')
| -rw-r--r-- | src/ui.rs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/ui.rs b/src/ui.rs new file mode 100644 index 0000000..bba01cc --- /dev/null +++ b/src/ui.rs @@ -0,0 +1,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)) +} |
