diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/channel/routes/mod.rs | 4 | ||||
| -rw-r--r-- | src/clock.rs | 1 | ||||
| -rw-r--r-- | src/event/extract.rs | 41 | ||||
| -rw-r--r-- | src/invite/routes/mod.rs | 4 | ||||
| -rw-r--r-- | src/message/routes/mod.rs | 2 | ||||
| -rw-r--r-- | src/token/extract/cookie.rs | 1 | ||||
| -rw-r--r-- | src/token/extract/identity.rs | 18 | ||||
| -rw-r--r-- | src/ui/routes/mod.rs | 6 |
8 files changed, 59 insertions, 18 deletions
diff --git a/src/channel/routes/mod.rs b/src/channel/routes/mod.rs index 696bd72..c1ef5cd 100644 --- a/src/channel/routes/mod.rs +++ b/src/channel/routes/mod.rs @@ -14,6 +14,6 @@ mod test; pub fn router() -> Router<App> { Router::new() .route("/api/channels", post(post::handler)) - .route("/api/channels/:channel", post(channel::post::handler)) - .route("/api/channels/:channel", delete(channel::delete::handler)) + .route("/api/channels/{channel}", post(channel::post::handler)) + .route("/api/channels/{channel}", delete(channel::delete::handler)) } diff --git a/src/clock.rs b/src/clock.rs index 242bcdf..acd0df0 100644 --- a/src/clock.rs +++ b/src/clock.rs @@ -21,7 +21,6 @@ impl RequestedAt { } } -#[async_trait::async_trait] impl<S> FromRequestParts<S> for RequestedAt where S: Send + Sync, diff --git a/src/event/extract.rs b/src/event/extract.rs index e3021e2..4a35937 100644 --- a/src/event/extract.rs +++ b/src/event/extract.rs @@ -1,7 +1,7 @@ use std::ops::Deref; use axum::{ - extract::FromRequestParts, + extract::{FromRequestParts, OptionalFromRequestParts}, http::{request::Parts, HeaderName, HeaderValue}, }; use axum_extra::typed_header::TypedHeader; @@ -44,7 +44,6 @@ where } } -#[async_trait::async_trait] impl<S, T> FromRequestParts<S> for LastEventId<T> where S: Send + Sync, @@ -53,12 +52,35 @@ where type Rejection = <TypedHeader<Self> as FromRequestParts<S>>::Rejection; async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { - // This is purely for ergonomics: it allows `RequestedAt` to be extracted - // without having to wrap it in `Extension<>`. Callers _can_ still do that, + // This is purely for ergonomics: it allows `LastEventId` to be extracted + // without having to wrap it in `TypedHeader<>`. Callers _can_ still do that, // but they aren't forced to. - let TypedHeader(requested_at) = TypedHeader::from_request_parts(parts, state).await?; + let header = + <TypedHeader<Self> as FromRequestParts<S>>::from_request_parts(parts, state).await?; - Ok(requested_at) + Ok(header.into()) + } +} + +impl<S, T> OptionalFromRequestParts<S> for LastEventId<T> +where + S: Send + Sync, + T: Serialize + DeserializeOwned, +{ + type Rejection = <TypedHeader<Self> as FromRequestParts<S>>::Rejection; + + async fn from_request_parts( + parts: &mut Parts, + state: &S, + ) -> Result<Option<Self>, Self::Rejection> { + // This is purely for ergonomics: it allows `Option<LastEventId>` to be extracted + // without having to wrap it in `TypedHeader<>`. Callers _can_ still do that, + // but they aren't forced to. + let header = + <TypedHeader<Self> as OptionalFromRequestParts<S>>::from_request_parts(parts, state) + .await?; + + Ok(header.map(Self::from)) } } @@ -71,6 +93,13 @@ impl<T> Deref for LastEventId<T> { } } +impl<T> From<TypedHeader<LastEventId<T>>> for LastEventId<T> { + fn from(header: TypedHeader<Self>) -> Self { + let TypedHeader(value) = header; + value + } +} + impl<T> From<T> for LastEventId<T> { fn from(value: T) -> Self { Self(value) diff --git a/src/invite/routes/mod.rs b/src/invite/routes/mod.rs index 2f7375c..a25dcd9 100644 --- a/src/invite/routes/mod.rs +++ b/src/invite/routes/mod.rs @@ -13,6 +13,6 @@ mod test; pub fn router() -> Router<App> { Router::new() .route("/api/invite", post(post::handler)) - .route("/api/invite/:invite", get(invite::get::handler)) - .route("/api/invite/:invite", post(invite::post::handler)) + .route("/api/invite/{invite}", get(invite::get::handler)) + .route("/api/invite/{invite}", post(invite::post::handler)) } diff --git a/src/message/routes/mod.rs b/src/message/routes/mod.rs index dfe8628..85c192f 100644 --- a/src/message/routes/mod.rs +++ b/src/message/routes/mod.rs @@ -5,5 +5,5 @@ use crate::app::App; mod message; pub fn router() -> Router<App> { - Router::new().route("/api/messages/:message", delete(message::delete::handler)) + Router::new().route("/api/messages/{message}", delete(message::delete::handler)) } diff --git a/src/token/extract/cookie.rs b/src/token/extract/cookie.rs index af5787d..df03aea 100644 --- a/src/token/extract/cookie.rs +++ b/src/token/extract/cookie.rs @@ -71,7 +71,6 @@ impl Identity { } } -#[async_trait::async_trait] impl<S> FromRequestParts<S> for Identity where S: Send + Sync, diff --git a/src/token/extract/identity.rs b/src/token/extract/identity.rs index a69f509..acfd7ae 100644 --- a/src/token/extract/identity.rs +++ b/src/token/extract/identity.rs @@ -1,5 +1,5 @@ use axum::{ - extract::{FromRequestParts, State}, + extract::{FromRequestParts, OptionalFromRequestParts, State}, http::request::Parts, response::{IntoResponse, Response}, }; @@ -20,7 +20,6 @@ pub struct Identity { pub login: Login, } -#[async_trait::async_trait] impl FromRequestParts<App> for Identity { type Rejection = LoginError<Internal>; @@ -39,6 +38,21 @@ impl FromRequestParts<App> for Identity { } } +impl OptionalFromRequestParts<App> for Identity { + type Rejection = LoginError<Internal>; + + async fn from_request_parts( + parts: &mut Parts, + state: &App, + ) -> Result<Option<Self>, Self::Rejection> { + match <Self as FromRequestParts<App>>::from_request_parts(parts, state).await { + Ok(identity) => Ok(Some(identity)), + Err(LoginError::Unauthorized) => Ok(None), + Err(other) => Err(other), + } + } +} + pub enum LoginError<E> { Failure(E), Unauthorized, diff --git a/src/ui/routes/mod.rs b/src/ui/routes/mod.rs index 48b3f90..7877dba 100644 --- a/src/ui/routes/mod.rs +++ b/src/ui/routes/mod.rs @@ -13,14 +13,14 @@ mod setup; pub fn router(app: &App) -> Router<App> { [ Router::new() - .route("/*path", get(path::get::handler)) + .route("/{*path}", get(path::get::handler)) .route("/setup", get(setup::get::handler)), Router::new() .route("/", get(get::handler)) .route("/me", get(me::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("/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() |
