summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorOwen Jacobson <owen@grimoire.ca>2024-10-11 01:18:18 -0400
committerOwen Jacobson <owen@grimoire.ca>2024-10-11 01:18:18 -0400
commit72f3d8c5ab3e2a42cf1a76d0c08815dbe46e50a1 (patch)
tree2b582e5319ac377649379851f8f5910517ca2a72 /src
parent2e2e3980ab78052be74f4007c343e69a583648fe (diff)
Move login to its own route.
This - in passing - fixes the problem where the client failed to subscribe after logging in, by causing the whole subscription process to be re-run when returning to the main interface.
Diffstat (limited to 'src')
-rw-r--r--src/ui.rs46
1 files changed, 33 insertions, 13 deletions
diff --git a/src/ui.rs b/src/ui.rs
index b296325..34eb6f1 100644
--- a/src/ui.rs
+++ b/src/ui.rs
@@ -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);