From 74f80d5c11d0a212a545f053bfd4ca160bcedcd8 Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Wed, 19 Feb 2025 21:22:03 -0500 Subject: Upgrade Axum to 0.8.1. --- src/channel/routes/mod.rs | 4 ++-- src/clock.rs | 1 - src/event/extract.rs | 41 +++++++++++++++++++++++++++++++++++------ src/invite/routes/mod.rs | 4 ++-- src/message/routes/mod.rs | 2 +- src/token/extract/cookie.rs | 1 - src/token/extract/identity.rs | 18 ++++++++++++++++-- src/ui/routes/mod.rs | 6 +++--- 8 files changed, 59 insertions(+), 18 deletions(-) (limited to 'src') 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 { 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 FromRequestParts 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 FromRequestParts for LastEventId where S: Send + Sync, @@ -53,12 +52,35 @@ where type Rejection = as FromRequestParts>::Rejection; async fn from_request_parts(parts: &mut Parts, state: &S) -> Result { - // 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 = + as FromRequestParts>::from_request_parts(parts, state).await?; - Ok(requested_at) + Ok(header.into()) + } +} + +impl OptionalFromRequestParts for LastEventId +where + S: Send + Sync, + T: Serialize + DeserializeOwned, +{ + type Rejection = as FromRequestParts>::Rejection; + + async fn from_request_parts( + parts: &mut Parts, + state: &S, + ) -> Result, Self::Rejection> { + // This is purely for ergonomics: it allows `Option` to be extracted + // without having to wrap it in `TypedHeader<>`. Callers _can_ still do that, + // but they aren't forced to. + let header = + as OptionalFromRequestParts>::from_request_parts(parts, state) + .await?; + + Ok(header.map(Self::from)) } } @@ -71,6 +93,13 @@ impl Deref for LastEventId { } } +impl From>> for LastEventId { + fn from(header: TypedHeader) -> Self { + let TypedHeader(value) = header; + value + } +} + impl From for LastEventId { 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 { 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 { - 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 FromRequestParts 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 for Identity { type Rejection = LoginError; @@ -39,6 +38,21 @@ impl FromRequestParts for Identity { } } +impl OptionalFromRequestParts for Identity { + type Rejection = LoginError; + + async fn from_request_parts( + parts: &mut Parts, + state: &App, + ) -> Result, Self::Rejection> { + match >::from_request_parts(parts, state).await { + Ok(identity) => Ok(Some(identity)), + Err(LoginError::Unauthorized) => Ok(None), + Err(other) => Err(other), + } + } +} + pub enum LoginError { 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 { [ 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() -- cgit v1.2.3