From 424fb08ecd315c67dd3862c29e87eea7bf32f65c Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Tue, 17 Jun 2025 00:46:24 -0400 Subject: Unify `setup_required` middlewares. The two middlewares were identical but for the specific `IntoResponse` impl used to generate the response when setup has not been completed. However, unifying them while still using `from_fn_with_state` lead to this horrorshow: .route_layer(middleware::from_fn_with_state( app.clone(), |state, req, next| { setup::middeware::setup_required(UNAVAILABLE, state, req, next) } )) It's a lot to read, and it surfaces the entire signature of a state-driven middleware `fn` into the call site solely to close over one argument (`UNAVAILABLE`). Rather than doing that, I've converted this middleware into a full blown Tower middleware, following . I considered taking this further and implementing a custom future to remove the allocation for `Box::pin`, but honestly, that allocation isn't hurting anyone and this code already got long enough in the translation. The new API looks like: .route_layer(setup::Required::or_unavailable(app.clone())) Or like: .route_layer(setup::Required::with_fallback(app.clone(), RESPONSE)) One thing I would have liked to have avoided is the additional `app.clone()` argument, but there isn't a way to extract the _state_ from a request inside of an Axum middleware. It has to be passed in externally - that's what `from_fn_with_state` is doing under the hood, as well. Using `State` as an extractor doesn't work; the `State` extractor is special in a _bunch_ of ways, and this is one of them. Other extractors would work. Realistically, I'd probably want to explore interfaces like .route_layer(setup::Required(app).or_unavailable()) or .route_layer(app.setup().required().or_unavailable()) --- src/cli.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'src/cli.rs') diff --git a/src/cli.rs b/src/cli.rs index 4232c00..042141d 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -15,12 +15,7 @@ use clap::{CommandFactory, Parser}; use sqlx::sqlite::SqlitePool; use tokio::net; -use crate::{ - app::App, - boot, channel, clock, db, event, expire, invite, message, - setup::{self, middleware::setup_required}, - ui, user, -}; +use crate::{app::App, boot, channel, clock, db, event, expire, invite, message, setup, ui, user}; /// Command-line entry point for running the `pilcrow` server. /// @@ -152,7 +147,7 @@ fn routers(app: &App) -> Router { app.clone(), expire::middleware, )) - .route_layer(middleware::from_fn_with_state(app.clone(), setup_required)), + .route_layer(setup::Required::or_unavailable(app.clone())), // API endpoints that handle setup setup::router(), // The UI (handles setup state itself) -- cgit v1.2.3 From 43375bcb875a31ce8c6132ce78552d45f64b261b Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Tue, 17 Jun 2025 01:22:54 -0400 Subject: Use a fluent style for the middleware layers. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For endpoints that are unavailable, that default behaviour no longer needs to be specified: `Required(app)` will do that for you. For endpoints that are redirects until setup is completed, `Require(app).with_fallback(…response…)` will do that. To make this a bit harder to break by accident, the default unavailable response is now its own type. --- src/cli.rs | 2 +- src/setup/mod.rs | 2 +- src/setup/required.rs | 53 ++++++++++++++++++++++++++++++++++----------------- src/ui/routes/mod.rs | 5 +---- 4 files changed, 39 insertions(+), 23 deletions(-) (limited to 'src/cli.rs') diff --git a/src/cli.rs b/src/cli.rs index 042141d..7bfdbc0 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -147,7 +147,7 @@ fn routers(app: &App) -> Router { app.clone(), expire::middleware, )) - .route_layer(setup::Required::or_unavailable(app.clone())), + .route_layer(setup::Required(app.clone())), // API endpoints that handle setup setup::router(), // The UI (handles setup state itself) diff --git a/src/setup/mod.rs b/src/setup/mod.rs index 62972b3..a4b821c 100644 --- a/src/setup/mod.rs +++ b/src/setup/mod.rs @@ -3,4 +3,4 @@ pub mod repo; mod required; mod routes; -pub use self::{required::Layer as Required, routes::router}; +pub use self::{required::Required, routes::router}; diff --git a/src/setup/required.rs b/src/setup/required.rs index 5b7fe5b..2112e4b 100644 --- a/src/setup/required.rs +++ b/src/setup/required.rs @@ -5,34 +5,40 @@ use axum::{ }; use std::pin::Pin; use std::task::{Context, Poll}; -use tower::Service; +use tower::{Layer, Service}; use crate::{app::App, error::Internal}; -const UNAVAILABLE: (StatusCode, &str) = ( - StatusCode::SERVICE_UNAVAILABLE, - "initial setup not completed", -); - #[derive(Clone)] -pub struct Layer { - app: App, - fallback: F, -} +pub struct Required(pub App); -impl Layer<(StatusCode, &'static str)> { - pub fn or_unavailable(app: App) -> Self { - Self::with_fallback(app, UNAVAILABLE) +impl Required { + pub fn with_fallback(self, fallback: F) -> WithFallback { + let Self(app) = self; + WithFallback { app, fallback } } } -impl Layer { - pub fn with_fallback(app: App, fallback: F) -> Self { - Layer { app, fallback } +impl Layer for Required { + type Service = Middleware; + + fn layer(&self, inner: S) -> Self::Service { + let Self(app) = self.clone(); + Middleware { + inner, + app, + fallback: Unavailable, + } } } -impl tower::Layer for Layer +#[derive(Clone)] +pub struct WithFallback { + app: App, + fallback: F, +} + +impl Layer for WithFallback where Self: Clone, { @@ -86,3 +92,16 @@ where }) } } + +#[derive(Clone)] +pub struct Unavailable; + +impl IntoResponse for Unavailable { + fn into_response(self) -> Response { + ( + StatusCode::SERVICE_UNAVAILABLE, + "initial setup not completed", + ) + .into_response() + } +} diff --git a/src/ui/routes/mod.rs b/src/ui/routes/mod.rs index 328eb73..dc94773 100644 --- a/src/ui/routes/mod.rs +++ b/src/ui/routes/mod.rs @@ -21,10 +21,7 @@ pub fn router(app: &App) -> Router { .route("/login", get(login::get::handler)) .route("/ch/{channel}", get(ch::channel::get::handler)) .route("/invite/{invite}", get(invite::invite::get::handler)) - .route_layer(crate::setup::Required::with_fallback( - app.clone(), - Redirect::to("/setup"), - )), + .route_layer(crate::setup::Required(app.clone()).with_fallback(Redirect::to("/setup"))), ] .into_iter() .fold(Router::default(), Router::merge) -- cgit v1.2.3