diff options
| author | ojacobson <ojacobson@noreply.codeberg.org> | 2025-06-18 23:52:47 +0200 |
|---|---|---|
| committer | ojacobson <ojacobson@noreply.codeberg.org> | 2025-06-18 23:52:47 +0200 |
| commit | d84ba5cd09b713fac2f193d5c05af7415ea6742d (patch) | |
| tree | 6ea93d417f329baccaa84f1d557638f54f3f2d37 /src/setup/mod.rs | |
| parent | 4a7fb2c5cf7265c5ef6a78051c1bb73e7d3ef086 (diff) | |
| parent | 43375bcb875a31ce8c6132ce78552d45f64b261b (diff) | |
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 <https://docs.rs/axum/latest/axum/middleware/index.html#towerservice-and-pinboxdyn-future>. 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(app.clone()))
Or like:
.route_layer(setup::Required(app.clone()).with_fallback(RESPONSE))
Part of a broader project to reorganize our routing. This is intended to reduce the number of "why do we have two of these?" questions when the answer isn't very compelling. We had two of these because I needed them to behave slightly differently, and at the time I didn't fully understand how to abstract out that difference. Now I do, thanks to Axum's excellent documentation.
Merges unify-setup-required into main.
Diffstat (limited to 'src/setup/mod.rs')
| -rw-r--r-- | src/setup/mod.rs | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/src/setup/mod.rs b/src/setup/mod.rs index 5a8fa37..a4b821c 100644 --- a/src/setup/mod.rs +++ b/src/setup/mod.rs @@ -1,6 +1,6 @@ pub mod app; -pub mod middleware; pub mod repo; +mod required; mod routes; -pub use self::routes::router; +pub use self::{required::Required, routes::router}; |
