diff options
Diffstat (limited to 'src/setup/required.rs')
| -rw-r--r-- | src/setup/required.rs | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/setup/required.rs b/src/setup/required.rs new file mode 100644 index 0000000..5b7fe5b --- /dev/null +++ b/src/setup/required.rs @@ -0,0 +1,88 @@ +use axum::{ + extract::Request, + http::StatusCode, + response::{IntoResponse, Response}, +}; +use std::pin::Pin; +use std::task::{Context, Poll}; +use tower::Service; + +use crate::{app::App, error::Internal}; + +const UNAVAILABLE: (StatusCode, &str) = ( + StatusCode::SERVICE_UNAVAILABLE, + "initial setup not completed", +); + +#[derive(Clone)] +pub struct Layer<F> { + app: App, + fallback: F, +} + +impl Layer<(StatusCode, &'static str)> { + pub fn or_unavailable(app: App) -> Self { + Self::with_fallback(app, UNAVAILABLE) + } +} + +impl<F> Layer<F> { + pub fn with_fallback(app: App, fallback: F) -> Self { + Layer { app, fallback } + } +} + +impl<S, F> tower::Layer<S> for Layer<F> +where + Self: Clone, +{ + type Service = Middleware<S, F>; + + fn layer(&self, inner: S) -> Self::Service { + let Self { app, fallback } = self.clone(); + Middleware { + inner, + app, + fallback, + } + } +} + +#[derive(Clone)] +pub struct Middleware<S, F> { + inner: S, + app: App, + fallback: F, +} + +impl<S, F> Service<Request> for Middleware<S, F> +where + Self: Clone, + S: Service<Request, Response = Response> + Send + 'static, + S::Future: Send, + F: IntoResponse + Clone + Send + 'static, +{ + type Response = S::Response; + type Error = S::Error; + type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>; + + fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { + self.inner.poll_ready(ctx) + } + + fn call(&mut self, req: Request) -> Self::Future { + let Self { + mut inner, + app, + fallback, + } = self.clone(); + + Box::pin(async move { + match app.setup().completed().await { + Ok(true) => inner.call(req).await, + Ok(false) => Ok(fallback.into_response()), + Err(error) => Ok(Internal::from(error).into_response()), + } + }) + } +} |
