summaryrefslogtreecommitdiff
path: root/src/setup/required.rs
blob: e475381cb0d6b62d431cd99e8963daaf0512e494 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use std::{
    pin::Pin,
    task::{Context, Poll},
};

use axum::{
    extract::{FromRef, Request},
    http::StatusCode,
    response::{IntoResponse, Response},
};
use tower::{Layer, Service};

use crate::{error::Internal, setup::app::Setup};

#[derive(Clone)]
pub struct Required<App>(pub App);

impl<App> Required<App> {
    pub fn with_fallback<F>(self, fallback: F) -> WithFallback<App, F> {
        let Self(app) = self;
        WithFallback { app, fallback }
    }
}

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();
        Middleware {
            inner,
            app,
            fallback: Unavailable,
        }
    }
}

#[derive(Clone)]
pub struct WithFallback<App, F> {
    app: App,
    fallback: F,
}

impl<S, App, F> Layer<S> for WithFallback<App, F>
where
    Self: Clone,
{
    type Service = Middleware<S, App, F>;

    fn layer(&self, inner: S) -> Self::Service {
        let Self { app, fallback } = self.clone();
        Middleware {
            inner,
            app,
            fallback,
        }
    }
}

#[derive(Clone)]
pub struct Middleware<S, App, F> {
    inner: S,
    app: App,
    fallback: 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;
    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 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()),
            }
        })
    }
}

#[derive(Clone)]
pub struct Unavailable;

impl IntoResponse for Unavailable {
    fn into_response(self) -> Response {
        (
            StatusCode::SERVICE_UNAVAILABLE,
            "initial setup not completed",
        )
            .into_response()
    }
}