use axum::{ extract::{Path, State}, http::{header, StatusCode}, response::{IntoResponse, Redirect, Response}, routing::get, Router, }; use mime_guess::Mime; use rust_embed::EmbeddedFile; use crate::{app::App, channel, error::Internal, login::Login}; #[derive(rust_embed::Embed)] #[folder = "hi-ui/build"] struct Assets; pub fn router() -> Router { Router::new() .route("/*path", get(asset)) .route("/", get(root)) .route("/ch/:channel", get(channel)) } async fn asset(Path(path): Path) -> Result> { let mime = mime_guess::from_path(&path).first_or_octet_stream(); Assets::get(&path) .map(|file| Asset(mime, file)) .ok_or(NotFound(format!("not found: {path}"))) } async fn root() -> Result { // "not found" in this case really is an internal error, as it should // never happen. `index.html` is a known-valid path. Ok(asset(Path(String::from("index.html"))).await?) } async fn channel( State(app): State, login: Option, Path(channel): Path, ) -> Result { Ok(if login.is_none() { Redirect::temporary("/").into_response() } else if app.channels().get(&channel).await?.is_none() { NotFound(root().await?).into_response() } else { root().await?.into_response() }) } struct Asset(Mime, EmbeddedFile); impl IntoResponse for Asset { fn into_response(self) -> Response { let Self(mime, file) = self; ( StatusCode::OK, [(header::CONTENT_TYPE, mime.as_ref())], file.data, ) .into_response() } } #[derive(Debug, thiserror::Error)] #[error("{0}")] struct NotFound(pub E); impl IntoResponse for NotFound where E: IntoResponse, { fn into_response(self) -> Response { let Self(response) = self; (StatusCode::NOT_FOUND, response).into_response() } }