|
@@ -6,15 +6,9 @@ use std::sync::Arc;
|
|
|
|
|
|
/// Settings for a fullstack app.
|
|
/// Settings for a fullstack app.
|
|
pub struct Config {
|
|
pub struct Config {
|
|
- #[cfg(feature = "server")]
|
|
|
|
- pub(crate) server_fn_route: &'static str,
|
|
|
|
-
|
|
|
|
#[cfg(feature = "server")]
|
|
#[cfg(feature = "server")]
|
|
pub(crate) server_cfg: ServeConfigBuilder,
|
|
pub(crate) server_cfg: ServeConfigBuilder,
|
|
|
|
|
|
- #[cfg(feature = "server")]
|
|
|
|
- pub(crate) addr: std::net::SocketAddr,
|
|
|
|
-
|
|
|
|
#[cfg(feature = "web")]
|
|
#[cfg(feature = "web")]
|
|
pub(crate) web_cfg: dioxus_web::Config,
|
|
pub(crate) web_cfg: dioxus_web::Config,
|
|
|
|
|
|
@@ -29,10 +23,6 @@ pub struct Config {
|
|
impl Default for Config {
|
|
impl Default for Config {
|
|
fn default() -> Self {
|
|
fn default() -> Self {
|
|
Self {
|
|
Self {
|
|
- #[cfg(feature = "server")]
|
|
|
|
- server_fn_route: "",
|
|
|
|
- #[cfg(feature = "server")]
|
|
|
|
- addr: std::net::SocketAddr::from(([0, 0, 0, 0], 8080)),
|
|
|
|
#[cfg(feature = "server")]
|
|
#[cfg(feature = "server")]
|
|
server_cfg: ServeConfigBuilder::new(),
|
|
server_cfg: ServeConfigBuilder::new(),
|
|
#[cfg(feature = "web")]
|
|
#[cfg(feature = "web")]
|
|
@@ -51,24 +41,6 @@ impl Config {
|
|
Self::default()
|
|
Self::default()
|
|
}
|
|
}
|
|
|
|
|
|
- /// Set the address to serve the app on.
|
|
|
|
- #[cfg(feature = "server")]
|
|
|
|
- #[cfg_attr(docsrs, doc(cfg(feature = "server")))]
|
|
|
|
- pub fn addr(self, addr: impl Into<std::net::SocketAddr>) -> Self {
|
|
|
|
- let addr = addr.into();
|
|
|
|
- Self { addr, ..self }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Set the route to the server functions.
|
|
|
|
- #[cfg(feature = "server")]
|
|
|
|
- #[cfg_attr(docsrs, doc(cfg(feature = "server")))]
|
|
|
|
- pub fn server_fn_route(self, server_fn_route: &'static str) -> Self {
|
|
|
|
- Self {
|
|
|
|
- server_fn_route,
|
|
|
|
- ..self
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/// Set the incremental renderer config.
|
|
/// Set the incremental renderer config.
|
|
#[cfg(feature = "server")]
|
|
#[cfg(feature = "server")]
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "server")))]
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "server")))]
|
|
@@ -82,77 +54,52 @@ impl Config {
|
|
/// Set the server config.
|
|
/// Set the server config.
|
|
#[cfg(feature = "server")]
|
|
#[cfg(feature = "server")]
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "server")))]
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "server")))]
|
|
- pub fn server_cfg(self, server_cfg: ServeConfigBuilder) -> Self {
|
|
|
|
|
|
+ pub fn with_server_cfg(self, server_cfg: ServeConfigBuilder) -> Self {
|
|
Self { server_cfg, ..self }
|
|
Self { server_cfg, ..self }
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /// Set the server config.
|
|
|
|
+ #[cfg(feature = "server")]
|
|
|
|
+ #[cfg_attr(docsrs, doc(cfg(feature = "server")))]
|
|
|
|
+ pub fn set_server_cfg(&mut self, server_cfg: ServeConfigBuilder) {
|
|
|
|
+ self.server_cfg = server_cfg;
|
|
|
|
+ }
|
|
|
|
+
|
|
/// Set the web config.
|
|
/// Set the web config.
|
|
#[cfg(feature = "web")]
|
|
#[cfg(feature = "web")]
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "web")))]
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "web")))]
|
|
- pub fn web_cfg(self, web_cfg: dioxus_web::Config) -> Self {
|
|
|
|
|
|
+ pub fn with_web_config(self, web_cfg: dioxus_web::Config) -> Self {
|
|
Self { web_cfg, ..self }
|
|
Self { web_cfg, ..self }
|
|
}
|
|
}
|
|
|
|
|
|
- /// Set the desktop config.
|
|
|
|
|
|
+ /// Set the web config.
|
|
|
|
+ #[cfg(feature = "web")]
|
|
|
|
+ #[cfg_attr(docsrs, doc(cfg(feature = "web")))]
|
|
|
|
+ pub fn set_web_config(&mut self, web_cfg: dioxus_web::Config) {
|
|
|
|
+ self.web_cfg = web_cfg;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// Set the desktop config
|
|
#[cfg(feature = "desktop")]
|
|
#[cfg(feature = "desktop")]
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "desktop")))]
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "desktop")))]
|
|
- pub fn desktop_cfg(self, desktop_cfg: dioxus_desktop::Config) -> Self {
|
|
|
|
|
|
+ pub fn with_desktop_config(self, desktop_cfg: dioxus_desktop::Config) -> Self {
|
|
Self {
|
|
Self {
|
|
desktop_cfg,
|
|
desktop_cfg,
|
|
..self
|
|
..self
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /// Set the desktop config.
|
|
|
|
+ #[cfg(feature = "desktop")]
|
|
|
|
+ #[cfg_attr(docsrs, doc(cfg(feature = "desktop")))]
|
|
|
|
+ pub fn set_desktop_config(&mut self, desktop_cfg: dioxus_desktop::Config) {
|
|
|
|
+ self.desktop_cfg = desktop_cfg;
|
|
|
|
+ }
|
|
|
|
+
|
|
/// Set the mobile config.
|
|
/// Set the mobile config.
|
|
#[cfg(feature = "mobile")]
|
|
#[cfg(feature = "mobile")]
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "mobile")))]
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "mobile")))]
|
|
- pub fn mobile_cfg(self, mobile_cfg: dioxus_mobile::Config) -> Self {
|
|
|
|
|
|
+ pub fn with_mobile_cfg(self, mobile_cfg: dioxus_mobile::Config) -> Self {
|
|
Self { mobile_cfg, ..self }
|
|
Self { mobile_cfg, ..self }
|
|
}
|
|
}
|
|
-
|
|
|
|
- #[cfg(feature = "server")]
|
|
|
|
- #[cfg_attr(docsrs, doc(cfg(feature = "server")))]
|
|
|
|
- /// Launch a server application
|
|
|
|
- pub async fn launch_server(
|
|
|
|
- self,
|
|
|
|
- build_virtual_dom: impl Fn() -> VirtualDom + Send + Sync + 'static,
|
|
|
|
- context_providers: crate::launch::ContextProviders,
|
|
|
|
- ) {
|
|
|
|
- use std::any::Any;
|
|
|
|
-
|
|
|
|
- let addr = self.addr;
|
|
|
|
- println!("Listening on http://{}", addr);
|
|
|
|
- let cfg = self.server_cfg.build();
|
|
|
|
- let server_fn_route = self.server_fn_route;
|
|
|
|
-
|
|
|
|
- #[cfg(feature = "axum")]
|
|
|
|
- {
|
|
|
|
- use crate::axum_adapter::{render_handler, DioxusRouterExt};
|
|
|
|
- use axum::routing::get;
|
|
|
|
- use tower::ServiceBuilder;
|
|
|
|
-
|
|
|
|
- let ssr_state = SSRState::new(&cfg);
|
|
|
|
- let router =
|
|
|
|
- axum::Router::new().register_server_functions_with_context(context_providers);
|
|
|
|
- #[cfg(not(any(feature = "desktop", feature = "mobile")))]
|
|
|
|
- let router = {
|
|
|
|
- let mut router = router.serve_static_assets(cfg.assets_path.clone()).await;
|
|
|
|
-
|
|
|
|
- #[cfg(all(feature = "hot-reload", debug_assertions))]
|
|
|
|
- {
|
|
|
|
- use dioxus_hot_reload::HotReloadRouterExt;
|
|
|
|
- router = router.forward_cli_hot_reloading();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- router.fallback(get(render_handler).with_state((
|
|
|
|
- cfg,
|
|
|
|
- Arc::new(build_virtual_dom),
|
|
|
|
- ssr_state,
|
|
|
|
- )))
|
|
|
|
- };
|
|
|
|
- let router = router.into_make_service();
|
|
|
|
- let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
|
|
|
|
- axum::serve(listener, router).await.unwrap();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
}
|
|
}
|