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
//! 

use core::convert::Infallible;
use core::future::*;
use core::pin::Pin;

use axum::serve::*;
use tower::Service;

use crate::res::*;
use crate::req::*;
use super::*;

type F1 = Pin<Box<dyn Future<Output = ()> + Send>>;
type Ca<S, State> = WithGracefulShutdown<AxumRouter<State>, S, F1>;

/// A Catalyzed application that is ready to be launched.
#[repr(transparent)]
#[allow(missing_debug_implementations)]
pub struct CatalyzedApp<S, State = ()>(Ca<S, State>) where
    State: Clone + Send + Sync + 'static,
    AxumRouter<State>: for<'a> Service<IncomingStream<'a>, Error = Infallible, Response = S> + Send + 'static,
    for<'a> <AxumRouter<State> as Service<IncomingStream<'a>>>::Future: Send,
    S: Service<RawRequest, Response = RawResponse, Error = Infallible> + Clone + Send + 'static,
    S::Future: Send;
impl<S, State> IntoFuture for CatalyzedApp<S, State> where
    State: Clone + Send + Sync + 'static,
    AxumRouter<State>: for<'a> Service<IncomingStream<'a>, Error = Infallible, Response = S> + Send + 'static,
    for<'a> <AxumRouter<State> as Service<IncomingStream<'a>>>::Future: Send,
    S: Service<RawRequest, Response = RawResponse, Error = Infallible> + Clone + Send + 'static,
    S::Future: Send, {
    type Output = <Ca<S, State> as IntoFuture>::Output;
    type IntoFuture = <Ca<S, State> as IntoFuture>::IntoFuture;
    #[inline] fn into_future(self) -> Self::IntoFuture { self.0.into_future() } }
impl<S, State> App<State> where
    State: Clone + Send + Sync + 'static,
    AxumRouter<State>: for<'a> Service<IncomingStream<'a>, Error = Infallible, Response = S> + Send + 'static,
    for<'a> <AxumRouter<State> as Service<IncomingStream<'a>>>::Future: Send,
    S: Service<RawRequest, Response = RawResponse, Error = Infallible> + Clone + Send + 'static,
    S::Future: Send,
{
    /// Catalyzes the application and launches it.
    /// 
    /// This should be the last method called on the [`App`](crate::App) instance.
    pub async fn launch(self) -> Result<CatalyzedApp<S, State>> {
        let addr = self.address.ok_or(CatalyzerError::NoAddress)?;
        let tcp = tokio::net::TcpListener::bind(addr).await?;
        let app = axum::serve(tcp, self.router);
        Ok(CatalyzedApp(app.with_graceful_shutdown(signal_handler())))
    }
}

#[inline]
fn signal_handler() -> Pin<Box<dyn Future<Output = ()> + Send>> {
    use super::runtime::signals::*;
    Box::pin(async { tokio::select! {
        _ = ctrl_c() => {},
        _ = term() => {},
    } })
}