summaryrefslogtreecommitdiff
path: root/src/ui
diff options
context:
space:
mode:
Diffstat (limited to 'src/ui')
-rw-r--r--src/ui/assets.rs63
-rw-r--r--src/ui/error.rs18
-rw-r--r--src/ui/middleware.rs15
-rw-r--r--src/ui/mime.rs22
-rw-r--r--src/ui/mod.rs7
-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
14 files changed, 365 insertions, 0 deletions
diff --git a/src/ui/assets.rs b/src/ui/assets.rs
new file mode 100644
index 0000000..6a7563a
--- /dev/null
+++ b/src/ui/assets.rs
@@ -0,0 +1,63 @@
+use ::mime::{FromStrError, Mime};
+use axum::{
+ http::{header, StatusCode},
+ response::{IntoResponse, Response},
+};
+use rust_embed::EmbeddedFile;
+
+use super::{error::NotFound, mime};
+use crate::error::Internal;
+
+#[derive(rust_embed::Embed)]
+#[folder = "target/ui"]
+pub struct Assets;
+
+impl Assets {
+ pub fn load(path: impl AsRef<str>) -> Result<Asset, Error> {
+ let path = path.as_ref();
+ let mime = mime::from_path(path)?;
+
+ Self::get(path)
+ .map(|file| Asset(mime, file))
+ .ok_or(Error::NotFound(path.into()))
+ }
+
+ pub 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 with a known-valid
+ // file extension.
+ Ok(Self::load("index.html")?)
+ }
+}
+
+pub 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)]
+pub enum Error {
+ #[error("not found: {0}")]
+ NotFound(String),
+ #[error(transparent)]
+ Mime(#[from] FromStrError),
+}
+
+impl IntoResponse for Error {
+ fn into_response(self) -> Response {
+ #[allow(clippy::match_wildcard_for_single_variants)]
+ match self {
+ Self::NotFound(_) => NotFound(self.to_string()).into_response(),
+ other => Internal::from(other).into_response(),
+ }
+ }
+}
diff --git a/src/ui/error.rs b/src/ui/error.rs
new file mode 100644
index 0000000..2dc627f
--- /dev/null
+++ b/src/ui/error.rs
@@ -0,0 +1,18 @@
+use axum::{
+ http::StatusCode,
+ response::{IntoResponse, Response},
+};
+
+#[derive(Debug, thiserror::Error)]
+#[error("{0}")]
+pub struct NotFound<E>(pub E);
+
+impl<E> IntoResponse for NotFound<E>
+where
+ E: IntoResponse,
+{
+ fn into_response(self) -> Response {
+ let Self(response) = self;
+ (StatusCode::NOT_FOUND, response).into_response()
+ }
+}
diff --git a/src/ui/middleware.rs b/src/ui/middleware.rs
new file mode 100644
index 0000000..f60ee1c
--- /dev/null
+++ b/src/ui/middleware.rs
@@ -0,0 +1,15 @@
+use axum::{
+ extract::{Request, State},
+ middleware::Next,
+ response::{IntoResponse, Redirect, Response},
+};
+
+use crate::{app::App, error::Internal};
+
+pub async fn setup_required(State(app): State<App>, request: Request, next: Next) -> Response {
+ match app.setup().completed().await {
+ Ok(true) => next.run(request).await,
+ Ok(false) => Redirect::to("/setup").into_response(),
+ Err(error) => Internal::from(error).into_response(),
+ }
+}
diff --git a/src/ui/mime.rs b/src/ui/mime.rs
new file mode 100644
index 0000000..9c724f0
--- /dev/null
+++ b/src/ui/mime.rs
@@ -0,0 +1,22 @@
+use mime::Mime;
+use unix_path::Path;
+
+// Extremely manual; using `std::path` here would result in platform-dependent behaviour when it's not appropriate (the URLs passed here always use `/` and are parsed like URLs). Using `unix_path` might be an option, but it's not clearly
+pub fn from_path<P>(path: P) -> Result<Mime, mime::FromStrError>
+where
+ P: AsRef<Path>,
+{
+ let path = path.as_ref();
+ let extension = path.extension().and_then(|ext| ext.to_str());
+ let mime = match extension {
+ Some("css") => "text/css; charset=utf-8",
+ Some("js") => "text/javascript; charset=utf-8",
+ Some("json") => "application/json",
+ Some("html") => "text/html; charset=utf-8",
+ Some("png") => "image/png",
+ _ => "application/octet-stream",
+ };
+ let mime = mime.parse()?;
+
+ Ok(mime)
+}
diff --git a/src/ui/mod.rs b/src/ui/mod.rs
new file mode 100644
index 0000000..f8caa48
--- /dev/null
+++ b/src/ui/mod.rs
@@ -0,0 +1,7 @@
+mod assets;
+mod error;
+mod middleware;
+mod mime;
+mod routes;
+
+pub use self::routes::router;
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(),
+ }
+ }
+ }
+}