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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//! This module contains the `launch` function, which is the main entry point for dioxus fullstack

use std::{any::Any, sync::Arc};

use dioxus_lib::prelude::{Element, VirtualDom};

pub use crate::Config;

#[allow(unused)]
pub(crate) type ContextProviders = Arc<
    Vec<Box<dyn Fn() -> Box<dyn std::any::Any + Send + Sync + 'static> + Send + Sync + 'static>>,
>;

#[allow(unused)]
fn virtual_dom_factory(
    root: fn() -> Element,
    contexts: ContextProviders,
) -> impl Fn() -> VirtualDom + 'static {
    move || {
        let mut vdom = VirtualDom::new(root);
        for context in &*contexts {
            vdom.insert_any_root_context(context());
        }
        vdom
    }
}

#[cfg(feature = "server")]
/// Launch a fullstack app with the given root component, contexts, and config.
#[allow(unused)]
pub fn launch(
    root: fn() -> Element,
    contexts: Vec<Box<dyn Fn() -> Box<dyn Any + Send + Sync> + Send + Sync>>,
    platform_config: Config,
) -> ! {
    let contexts = Arc::new(contexts);
    let factory = virtual_dom_factory(root, contexts.clone());
    #[cfg(all(feature = "server", not(target_arch = "wasm32")))]
    tokio::runtime::Runtime::new()
        .unwrap()
        .block_on(async move {
            launch_server(platform_config, factory, contexts).await;
        });

    unreachable!("Launching a fullstack app should never return")
}

#[cfg(all(not(feature = "server"), feature = "web"))]
/// Launch a fullstack app with the given root component, contexts, and config.
#[allow(unused)]
pub fn launch(
    root: fn() -> Element,
    #[allow(unused_mut)] mut contexts: Vec<
        Box<dyn Fn() -> Box<dyn Any + Send + Sync> + Send + Sync>,
    >,
    platform_config: Config,
) {
    let contexts = Arc::new(contexts);
    let mut factory = virtual_dom_factory(root, contexts);
    let cfg = platform_config.web_cfg.hydrate(true);

    #[cfg(feature = "document")]
    let factory = move || {
        let mut vdom = factory();
        let document = std::rc::Rc::new(crate::document::web::FullstackWebDocument)
            as std::rc::Rc<dyn dioxus_lib::prelude::document::Document>;
        vdom.provide_root_context(document);
        vdom
    };

    dioxus_web::launch::launch_virtual_dom(factory(), cfg)
}

#[cfg(all(not(any(feature = "server", feature = "web")), feature = "desktop"))]
/// Launch a fullstack app with the given root component, contexts, and config.
#[allow(unused)]
pub fn launch(
    root: fn() -> Element,
    contexts: Vec<Box<dyn Fn() -> Box<dyn Any + Send + Sync> + Send + Sync>>,
    platform_config: Config,
) -> ! {
    let contexts = Arc::new(contexts);
    let factory = virtual_dom_factory(root, contexts);
    let cfg = platform_config.desktop_cfg;
    dioxus_desktop::launch::launch_virtual_dom(factory(), cfg)
}

#[cfg(all(
    not(any(feature = "server", feature = "web", feature = "desktop")),
    feature = "mobile"
))]
/// Launch a fullstack app with the given root component, contexts, and config.
#[allow(unused)]
pub fn launch(
    root: fn() -> Element,
    contexts: Vec<Box<dyn Fn() -> Box<dyn Any + Send + Sync> + Send + Sync>>,
    platform_config: Config,
) -> ! {
    let contexts = Arc::new(contexts);
    let factory = virtual_dom_factory(root, contexts.clone());
    let cfg = platform_config.mobile_cfg;
    dioxus_mobile::launch::launch_virtual_dom(factory(), cfg)
}

#[cfg(not(any(
    feature = "server",
    feature = "web",
    feature = "desktop",
    feature = "mobile"
)))]
/// Launch a fullstack app with the given root component, contexts, and config.
#[allow(unused)]
pub fn launch(
    root: fn() -> Element,
    contexts: Vec<Box<dyn Fn() -> Box<dyn Any + Send + Sync> + Send + Sync>>,
    platform_config: Config,
) -> ! {
    panic!("No platform feature enabled. Please enable one of the following features: axum, desktop, or web to use the launch API.")
}

#[cfg(feature = "server")]
#[allow(unused)]
/// Launch a server application
async fn launch_server(
    platform_config: Config,
    build_virtual_dom: impl Fn() -> VirtualDom + Send + Sync + 'static,
    context_providers: ContextProviders,
) {
    use clap::Parser;

    // Get the address the server should run on. If the CLI is running, the CLI proxies fullstack into the main address
    // and we use the generated address the CLI gives us
    let cli_args = dioxus_cli_config::RuntimeCLIArguments::from_cli();
    let address = cli_args
        .as_ref()
        .map(|args| args.fullstack_address())
        .unwrap_or_else(dioxus_cli_config::AddressArguments::parse)
        .address();

    // Point the user to the CLI address if the CLI is running or the fullstack address if not
    let serve_address = cli_args
        .map(|args| args.cli_address())
        .unwrap_or_else(|| address);

    #[cfg(feature = "axum")]
    {
        use crate::axum_adapter::DioxusRouterExt;

        let router = axum::Router::new().register_server_functions_with_context(context_providers);

        #[cfg(not(any(feature = "desktop", feature = "mobile")))]
        let router = {
            use crate::prelude::RenderHandleState;
            use crate::prelude::SSRState;

            let cfg = platform_config.server_cfg.build();

            let mut router = router.serve_static_assets();

            router.fallback(
                axum::routing::get(crate::axum_adapter::render_handler).with_state(
                    RenderHandleState::new_with_virtual_dom_factory(build_virtual_dom)
                        .with_config(cfg),
                ),
            )
        };

        let router = router.into_make_service();
        let listener = tokio::net::TcpListener::bind(address).await.unwrap();

        axum::serve(listener, router).await.unwrap();
    }
    #[cfg(not(feature = "axum"))]
    {
        panic!("Launching with dioxus fullstack requires the axum feature. If you are using a community fullstack adapter, please check the documentation for that adapter to see how to launch the application.");
    }
}