summaryrefslogtreecommitdiff
path: root/src/ui/routes
diff options
context:
space:
mode:
Diffstat (limited to 'src/ui/routes')
-rw-r--r--src/ui/routes/ch/channel.rs61
-rw-r--r--src/ui/routes/ch/mod.rs1
-rw-r--r--src/ui/routes/get.rs30
-rw-r--r--src/ui/routes/invite/invite.rs55
-rw-r--r--src/ui/routes/invite/mod.rs4
-rw-r--r--src/ui/routes/login.rs11
-rw-r--r--src/ui/routes/mod.rs26
-rw-r--r--src/ui/routes/path.rs9
-rw-r--r--src/ui/routes/setup.rs43
9 files changed, 240 insertions, 0 deletions
diff --git a/src/ui/routes/ch/channel.rs b/src/ui/routes/ch/channel.rs
new file mode 100644
index 0000000..a338f1f
--- /dev/null
+++ b/src/ui/routes/ch/channel.rs
@@ -0,0 +1,61 @@
+pub mod get {
+ use axum::{
+ extract::{Path, State},
+ response::{self, IntoResponse, Redirect},
+ };
+
+ use crate::{
+ app::App,
+ channel,
+ error::Internal,
+ token::extract::Identity,
+ ui::{
+ assets::{Asset, Assets},
+ error::NotFound,
+ },
+ };
+
+ pub async fn handler(
+ State(app): State<App>,
+ identity: Option<Identity>,
+ Path(channel): Path<channel::Id>,
+ ) -> Result<Asset, Error> {
+ let _ = identity.ok_or(Error::NotLoggedIn)?;
+ app.channels()
+ .get(&channel)
+ .await
+ .map_err(Error::internal)?
+ .ok_or(Error::NotFound)?;
+
+ Assets::index().map_err(Error::Internal)
+ }
+
+ #[derive(Debug, thiserror::Error)]
+ pub enum Error {
+ #[error("requested channel not found")]
+ NotFound,
+ #[error("not logged in")]
+ NotLoggedIn,
+ #[error("{0}")]
+ Internal(Internal),
+ }
+
+ impl Error {
+ fn internal(err: impl Into<Internal>) -> Self {
+ Self::Internal(err.into())
+ }
+ }
+
+ impl IntoResponse for Error {
+ fn into_response(self) -> response::Response {
+ match self {
+ Self::NotFound => match Assets::index() {
+ Ok(asset) => NotFound(asset).into_response(),
+ Err(internal) => internal.into_response(),
+ },
+ Self::NotLoggedIn => Redirect::temporary("/login").into_response(),
+ Self::Internal(error) => error.into_response(),
+ }
+ }
+ }
+}
diff --git a/src/ui/routes/ch/mod.rs b/src/ui/routes/ch/mod.rs
new file mode 100644
index 0000000..ff02972
--- /dev/null
+++ b/src/ui/routes/ch/mod.rs
@@ -0,0 +1 @@
+pub mod channel;
diff --git a/src/ui/routes/get.rs b/src/ui/routes/get.rs
new file mode 100644
index 0000000..2fcb51c
--- /dev/null
+++ b/src/ui/routes/get.rs
@@ -0,0 +1,30 @@
+use axum::response::{self, IntoResponse, Redirect};
+
+use crate::{
+ error::Internal,
+ token::extract::Identity,
+ ui::assets::{Asset, Assets},
+};
+
+pub async fn handler(identity: Option<Identity>) -> Result<Asset, Error> {
+ let _ = identity.ok_or(Error::NotLoggedIn)?;
+
+ Assets::index().map_err(Error::Internal)
+}
+
+#[derive(Debug, thiserror::Error)]
+pub enum Error {
+ #[error("not logged in")]
+ NotLoggedIn,
+ #[error("{0}")]
+ Internal(Internal),
+}
+
+impl IntoResponse for Error {
+ fn into_response(self) -> response::Response {
+ match self {
+ Self::NotLoggedIn => Redirect::temporary("/login").into_response(),
+ Self::Internal(error) => error.into_response(),
+ }
+ }
+}
diff --git a/src/ui/routes/invite/invite.rs b/src/ui/routes/invite/invite.rs
new file mode 100644
index 0000000..06e5792
--- /dev/null
+++ b/src/ui/routes/invite/invite.rs
@@ -0,0 +1,55 @@
+pub mod get {
+ use axum::{
+ extract::{Path, State},
+ response::{self, IntoResponse},
+ };
+
+ use crate::{
+ app::App,
+ error::Internal,
+ invite,
+ ui::{
+ assets::{Asset, Assets},
+ error::NotFound,
+ },
+ };
+
+ pub async fn handler(
+ State(app): State<App>,
+ Path(invite): Path<invite::Id>,
+ ) -> Result<Asset, Error> {
+ app.invites()
+ .get(&invite)
+ .await
+ .map_err(Error::internal)?
+ .ok_or(Error::NotFound)?;
+
+ Assets::index().map_err(Error::Internal)
+ }
+
+ #[derive(Debug, thiserror::Error)]
+ pub enum Error {
+ #[error("invite not found")]
+ NotFound,
+ #[error("{0}")]
+ Internal(Internal),
+ }
+
+ impl Error {
+ fn internal(err: impl Into<Internal>) -> Self {
+ Self::Internal(err.into())
+ }
+ }
+
+ impl IntoResponse for Error {
+ fn into_response(self) -> response::Response {
+ match self {
+ Self::NotFound => match Assets::index() {
+ Ok(asset) => NotFound(asset).into_response(),
+ Err(internal) => internal.into_response(),
+ },
+ Self::Internal(error) => error.into_response(),
+ }
+ }
+ }
+}
diff --git a/src/ui/routes/invite/mod.rs b/src/ui/routes/invite/mod.rs
new file mode 100644
index 0000000..50af8be
--- /dev/null
+++ b/src/ui/routes/invite/mod.rs
@@ -0,0 +1,4 @@
+// In this case, the first redundant `invite` is a literal path segment, and the
+// second `invite` reflects a placeholder.
+#[allow(clippy::module_inception)]
+pub mod invite;
diff --git a/src/ui/routes/login.rs b/src/ui/routes/login.rs
new file mode 100644
index 0000000..81a874c
--- /dev/null
+++ b/src/ui/routes/login.rs
@@ -0,0 +1,11 @@
+pub mod get {
+ use crate::{
+ error::Internal,
+ ui::assets::{Asset, Assets},
+ };
+
+ #[allow(clippy::unused_async)]
+ pub async fn handler() -> Result<Asset, Internal> {
+ Assets::index()
+ }
+}
diff --git a/src/ui/routes/mod.rs b/src/ui/routes/mod.rs
new file mode 100644
index 0000000..72d9a4a
--- /dev/null
+++ b/src/ui/routes/mod.rs
@@ -0,0 +1,26 @@
+use axum::{middleware, routing::get, Router};
+
+use crate::{app::App, ui::middleware::setup_required};
+
+mod ch;
+mod get;
+mod invite;
+mod login;
+mod path;
+mod setup;
+
+pub fn router(app: &App) -> Router<App> {
+ [
+ Router::new()
+ .route("/*path", get(path::get::handler))
+ .route("/setup", get(setup::get::handler)),
+ Router::new()
+ .route("/", get(get::handler))
+ .route("/login", get(login::get::handler))
+ .route("/ch/:channel", get(ch::channel::get::handler))
+ .route("/invite/:invite", get(invite::invite::get::handler))
+ .route_layer(middleware::from_fn_with_state(app.clone(), setup_required)),
+ ]
+ .into_iter()
+ .fold(Router::default(), Router::merge)
+}
diff --git a/src/ui/routes/path.rs b/src/ui/routes/path.rs
new file mode 100644
index 0000000..a387552
--- /dev/null
+++ b/src/ui/routes/path.rs
@@ -0,0 +1,9 @@
+pub mod get {
+ use axum::extract::Path;
+
+ use crate::ui::assets::{Asset, Assets, Error};
+
+ pub async fn handler(Path(path): Path<String>) -> Result<Asset, Error> {
+ Assets::load(path)
+ }
+}
diff --git a/src/ui/routes/setup.rs b/src/ui/routes/setup.rs
new file mode 100644
index 0000000..649cc5f
--- /dev/null
+++ b/src/ui/routes/setup.rs
@@ -0,0 +1,43 @@
+pub mod get {
+ use axum::{
+ extract::State,
+ response::{self, IntoResponse, Redirect},
+ };
+
+ use crate::{
+ app::App,
+ error::Internal,
+ ui::assets::{Asset, Assets},
+ };
+
+ pub async fn handler(State(app): State<App>) -> Result<Asset, Error> {
+ if app
+ .setup()
+ .completed()
+ .await
+ .map_err(Internal::from)
+ .map_err(Error::Internal)?
+ {
+ Err(Error::SetupCompleted)
+ } else {
+ Assets::index().map_err(Error::Internal)
+ }
+ }
+
+ #[derive(Debug, thiserror::Error)]
+ pub enum Error {
+ #[error("setup already completed")]
+ SetupCompleted,
+ #[error("{0}")]
+ Internal(Internal),
+ }
+
+ impl IntoResponse for Error {
+ fn into_response(self) -> response::Response {
+ match self {
+ Self::SetupCompleted => Redirect::to("/login").into_response(),
+ Self::Internal(error) => error.into_response(),
+ }
+ }
+ }
+}