summaryrefslogtreecommitdiff
path: root/src/setup/required.rs
diff options
context:
space:
mode:
authorOwen Jacobson <owen@grimoire.ca>2025-10-27 18:15:25 -0400
committerOwen Jacobson <owen@grimoire.ca>2025-10-28 01:43:26 -0400
commitd66728889105f6f1ef5113d9ceb223e362df0008 (patch)
tree5102813fbaa222276dcabd15a736838f9641d71f /src/setup/required.rs
parentc2a3a010c67776b9a459d7ba0930630ff25a3a51 (diff)
Convert the `Setup` component into a freestanding struct.
The changes to the setup-requiring middleware are probably more general than was strictly needed, but they will make it work with anything that can provide a `Setup` component rather than being bolted to `App` specifically, which feels tidier.
Diffstat (limited to 'src/setup/required.rs')
-rw-r--r--src/setup/required.rs31
1 files changed, 18 insertions, 13 deletions
diff --git a/src/setup/required.rs b/src/setup/required.rs
index a2aed18..e475381 100644
--- a/src/setup/required.rs
+++ b/src/setup/required.rs
@@ -4,26 +4,29 @@ use std::{
};
use axum::{
- extract::Request,
+ extract::{FromRef, Request},
http::StatusCode,
response::{IntoResponse, Response},
};
use tower::{Layer, Service};
-use crate::{app::App, error::Internal};
+use crate::{error::Internal, setup::app::Setup};
#[derive(Clone)]
-pub struct Required(pub App);
+pub struct Required<App>(pub App);
-impl Required {
- pub fn with_fallback<F>(self, fallback: F) -> WithFallback<F> {
+impl<App> Required<App> {
+ pub fn with_fallback<F>(self, fallback: F) -> WithFallback<App, F> {
let Self(app) = self;
WithFallback { app, fallback }
}
}
-impl<S> Layer<S> for Required {
- type Service = Middleware<S, Unavailable>;
+impl<S, App> Layer<S> for Required<App>
+where
+ Self: Clone,
+{
+ type Service = Middleware<S, App, Unavailable>;
fn layer(&self, inner: S) -> Self::Service {
let Self(app) = self.clone();
@@ -36,16 +39,16 @@ impl<S> Layer<S> for Required {
}
#[derive(Clone)]
-pub struct WithFallback<F> {
+pub struct WithFallback<App, F> {
app: App,
fallback: F,
}
-impl<S, F> Layer<S> for WithFallback<F>
+impl<S, App, F> Layer<S> for WithFallback<App, F>
where
Self: Clone,
{
- type Service = Middleware<S, F>;
+ type Service = Middleware<S, App, F>;
fn layer(&self, inner: S) -> Self::Service {
let Self { app, fallback } = self.clone();
@@ -58,17 +61,19 @@ where
}
#[derive(Clone)]
-pub struct Middleware<S, F> {
+pub struct Middleware<S, App, F> {
inner: S,
app: App,
fallback: F,
}
-impl<S, F> Service<Request> for Middleware<S, F>
+impl<S, App, F> Service<Request> for Middleware<S, App, F>
where
+ Setup: FromRef<App>,
Self: Clone,
S: Service<Request, Response = Response> + Send + 'static,
S::Future: Send,
+ App: Send + 'static,
F: IntoResponse + Clone + Send + 'static,
{
type Response = S::Response;
@@ -87,7 +92,7 @@ where
} = self.clone();
Box::pin(async move {
- match app.setup().completed().await {
+ match Setup::from_ref(&app).completed().await {
Ok(true) => inner.call(req).await,
Ok(false) => Ok(fallback.into_response()),
Err(error) => Ok(Internal::from(error).into_response()),