summaryrefslogtreecommitdiff
path: root/src/setup/required.rs
blob: 5b7fe5bd23fc593ecbeaef67078666fe41a6ab1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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()),
            }
        })
    }
}