diff options
Diffstat (limited to 'src/ui.rs')
| -rw-r--r-- | src/ui.rs | 46 |
1 files changed, 33 insertions, 13 deletions
@@ -14,25 +14,45 @@ use crate::{app::App, channel, error::Internal, login::Login}; #[folder = "target/ui"] struct Assets; +impl Assets { + fn load(path: impl AsRef<str>) -> Result<Asset, NotFound<String>> { + let path = path.as_ref(); + let mime = mime_guess::from_path(path).first_or_octet_stream(); + + Self::get(path) + .map(|file| Asset(mime, file)) + .ok_or(NotFound(format!("not found: {path}"))) + } + + fn index() -> Result<Asset, Internal> { + // "not found" in this case really is an internal error, as it should + // never happen. `index.html` is a known-valid path. + Ok(Self::load("index.html")?) + } +} + pub fn router() -> Router<App> { Router::new() .route("/*path", get(asset)) .route("/", get(root)) + .route("/login", get(login)) .route("/ch/:channel", get(channel)) } async fn asset(Path(path): Path<String>) -> Result<Asset, NotFound<String>> { - let mime = mime_guess::from_path(&path).first_or_octet_stream(); + Assets::load(path) +} - Assets::get(&path) - .map(|file| Asset(mime, file)) - .ok_or(NotFound(format!("not found: {path}"))) +async fn root(login: Option<Login>) -> Result<impl IntoResponse, Internal> { + if login.is_none() { + Ok(Redirect::temporary("/login").into_response()) + } else { + Ok(Assets::index()?.into_response()) + } } -async fn root() -> Result<Asset, Internal> { - // "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 login() -> Result<impl IntoResponse, Internal> { + Assets::index() } async fn channel( @@ -40,13 +60,13 @@ async fn channel( login: Option<Login>, Path(channel): Path<channel::Id>, ) -> Result<impl IntoResponse, Internal> { - Ok(if login.is_none() { - Redirect::temporary("/").into_response() + if login.is_none() { + Ok(Redirect::temporary("/").into_response()) } else if app.channels().get(&channel).await?.is_none() { - NotFound(root().await?).into_response() + Ok(NotFound(Assets::index()?).into_response()) } else { - root().await?.into_response() - }) + Ok(Assets::index()?.into_response()) + } } struct Asset(Mime, EmbeddedFile); |
