dioxus_aws/
lib.rs

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use dioxus::dioxus_core::Element;

#[cfg(feature = "server")]
use axum::routing::Route;
#[cfg(feature = "server")]
use axum_core::{extract::Request, response::IntoResponse};
#[cfg(feature = "server")]
use std::convert::Infallible;
#[cfg(feature = "server")]
use tower_layer::Layer;
#[cfg(feature = "server")]
use tower_service::Service;

#[cfg(feature = "lambda")]
mod lambda;

#[doc = include_str!("../docs/launch.md")]
pub fn launch(_app: fn() -> Element) {
    #[cfg(feature = "web")]
    dioxus::launch(_app);

    #[cfg(feature = "server")]
    {
        use axum::routing::*;
        use dioxus_fullstack::prelude::*;

        struct TryIntoResult(Result<ServeConfig, dioxus_fullstack::UnableToLoadIndex>);

        impl TryInto<ServeConfig> for TryIntoResult {
            type Error = dioxus_fullstack::UnableToLoadIndex;

            fn try_into(self) -> Result<ServeConfig, Self::Error> {
                self.0
            }
        }

        tokio::runtime::Runtime::new()
            .unwrap()
            .block_on(async move {
                let app = Router::new().serve_dioxus_application(
                    TryIntoResult(ServeConfigBuilder::default().build()),
                    _app,
                );

                #[cfg(not(feature = "lambda"))]
                {
                    let address = dioxus_cli_config::fullstack_address_or_localhost();
                    let listener = tokio::net::TcpListener::bind(address).await.unwrap();

                    axum::serve(listener, app.into_make_service())
                        .await
                        .unwrap();
                }

                #[cfg(feature = "lambda")]
                {
                    use self::lambda::LambdaAdapter;

                    tracing::info!("Running in lambda mode");
                    lambda_runtime::run(LambdaAdapter::from(app)).await.unwrap();
                }
            });
    };
}

#[cfg(feature = "server")]
pub async fn launch_with_layers<L>(app: fn() -> Element, layers: Vec<L>)
where
    L: Layer<Route> + Clone + Send + 'static,
    L::Service: Service<Request> + Clone + Send + 'static,
    <L::Service as Service<Request>>::Response: IntoResponse + 'static,
    <L::Service as Service<Request>>::Error: Into<Infallible> + 'static,
    <L::Service as Service<Request>>::Future: Send + 'static,
{
    #[cfg(feature = "web")]
    dioxus::launch(app);

    #[cfg(feature = "server")]
    {
        use axum::routing::*;
        use dioxus_fullstack::prelude::*;

        struct TryIntoResult(Result<ServeConfig, dioxus_fullstack::UnableToLoadIndex>);

        impl TryInto<ServeConfig> for TryIntoResult {
            type Error = dioxus_fullstack::UnableToLoadIndex;

            fn try_into(self) -> Result<ServeConfig, Self::Error> {
                self.0
            }
        }

        let mut app = Router::new()
            .serve_dioxus_application(TryIntoResult(ServeConfigBuilder::default().build()), app);

        for layer in layers {
            app = app.layer(layer);
        }

        #[cfg(not(feature = "lambda"))]
        {
            let address = dioxus_cli_config::fullstack_address_or_localhost();
            let listener = tokio::net::TcpListener::bind(address).await.unwrap();

            axum::serve(listener, app.into_make_service())
                .await
                .unwrap();
        }

        #[cfg(feature = "lambda")]
        {
            use self::lambda::LambdaAdapter;

            tracing::info!("Running in lambda mode");
            lambda_runtime::run(LambdaAdapter::from(app)).await.unwrap();
        }
    };
}

#[cfg(feature = "server")]
pub struct AppBuilder<L> {
    layers: Vec<L>,
    app: fn() -> Element,
}

#[cfg(feature = "server")]
impl<L> AppBuilder<L>
where
    L: Layer<Route> + Clone + Send + 'static,
    L::Service: Service<Request> + Clone + Send + 'static,
    <L::Service as Service<Request>>::Response: IntoResponse + 'static,
    <L::Service as Service<Request>>::Error: Into<Infallible> + 'static,
    <L::Service as Service<Request>>::Future: Send + 'static,
{
    pub fn new(app: fn() -> Element) -> Self {
        Self {
            layers: Vec::new(),
            app,
        }
    }

    pub fn layer(mut self, layer: L) -> Self {
        self.layers.push(layer);
        self
    }

    pub async fn serve(self) {
        launch_with_layers(self.app, self.layers).await;
    }
}