diff options
| author | Owen Jacobson <owen@grimoire.ca> | 2024-10-11 15:52:42 -0400 |
|---|---|---|
| committer | Owen Jacobson <owen@grimoire.ca> | 2024-10-11 15:52:42 -0400 |
| commit | d33c8af14c4adc1c15ab048299e06f9f35ae4de6 (patch) | |
| tree | 5dce8c28876353893dfd4725dcd093afff7e6dff /src | |
| parent | 7bcfeb08946e64642b33f4b099ff235ba8527697 (diff) | |
| parent | 72f3d8c5ab3e2a42cf1a76d0c08815dbe46e50a1 (diff) | |
Merge branch 'wip/login-route'
Diffstat (limited to 'src')
| -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); |
