From 1be48c4aa8cdb45f5a35014e59c4f62b8d21a906 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Thu, 30 Mar 2023 11:03:07 -0500 Subject: [PATCH] create a serveconfig builder --- .../server/examples/hello-world/src/main.rs | 7 +-- packages/server/src/adapters/axum_adapter.rs | 26 ++-------- packages/server/src/lib.rs | 33 ++++++++----- packages/server/src/serve.rs | 47 +++++++++++++++++++ 4 files changed, 74 insertions(+), 39 deletions(-) create mode 100644 packages/server/src/serve.rs diff --git a/packages/server/examples/hello-world/src/main.rs b/packages/server/examples/hello-world/src/main.rs index 9b9084963..9cafe93b5 100644 --- a/packages/server/examples/hello-world/src/main.rs +++ b/packages/server/examples/hello-world/src/main.rs @@ -17,12 +17,7 @@ fn main() { .serve( axum::Router::new() .serve_dioxus_application( - "Hello, world!", - "hello-world", - None, - None, - "", - app, + ServeConfig::new(app).head(r#"Hello World!"#), ) .into_make_service(), ) diff --git a/packages/server/src/adapters/axum_adapter.rs b/packages/server/src/adapters/axum_adapter.rs index 70513bdb3..649629f61 100644 --- a/packages/server/src/adapters/axum_adapter.rs +++ b/packages/server/src/adapters/axum_adapter.rs @@ -7,26 +7,18 @@ use axum::{ routing::{get, post}, Router, }; -use dioxus_core::Component; use server_fn::{Payload, ServerFunctionRegistry}; use tokio::task::spawn_blocking; use crate::{ dioxus_ssr_html, + serve::ServeConfig, server_fn::{DioxusServerContext, DioxusServerFnRegistry, ServerFnTraitObj}, }; pub trait DioxusRouterExt { fn register_server_fns(self, server_fn_route: &'static str) -> Self; - fn serve_dioxus_application( - self, - title: &'static str, - application_name: &'static str, - base_path: Option<&'static str>, - head: Option<&'static str>, - server_fn_route: &'static str, - app: Component, - ) -> Self; + fn serve_dioxus_application(self, cfg: ServeConfig) -> Self; } impl DioxusRouterExt for Router { @@ -45,24 +37,16 @@ impl DioxusRouterExt for Router { router } - fn serve_dioxus_application( - self, - title: &'static str, - application_name: &'static str, - base_path: Option<&'static str>, - head: Option<&'static str>, - server_fn_route: &'static str, - app: Component, - ) -> Self { + fn serve_dioxus_application(self, cfg: ServeConfig) -> Self { use tower_http::services::ServeDir; // Serve the dist folder and the index.html file let serve_dir = ServeDir::new("dist"); - self.register_server_fns(server_fn_route) + self.register_server_fns(cfg.server_fn_route.unwrap_or_default()) .nest_service("/", serve_dir) .fallback_service(get(move || { - let rendered = dioxus_ssr_html(title, application_name, base_path, head, app); + let rendered = dioxus_ssr_html(cfg); async move { Full::from(rendered) } })) } diff --git a/packages/server/src/lib.rs b/packages/server/src/lib.rs index c78f3471b..e14a445b5 100644 --- a/packages/server/src/lib.rs +++ b/packages/server/src/lib.rs @@ -2,38 +2,47 @@ use dioxus_core::prelude::*; mod adapters; +mod serve; mod server_fn; pub mod prelude { #[cfg(feature = "axum")] pub use crate::adapters::axum_adapter::*; + pub use crate::serve::ServeConfig; pub use crate::server_fn::{DioxusServerContext, ServerFn}; pub use server_fn::{self, ServerFn as _, ServerFnError}; pub use server_macro::*; } #[cfg(feature = "ssr")] -fn dioxus_ssr_html( - title: &str, - application_name: &str, - base_path: Option<&str>, - head: Option<&str>, - app: Component, -) -> String { +fn dioxus_ssr_html(cfg: serve::ServeConfig) -> String { + use prelude::ServeConfig; + + let ServeConfig { + app, + application_name, + base_path, + head, + .. + } = cfg; + + let application_name = application_name.unwrap_or("dioxus"); + let mut vdom = VirtualDom::new(app); let _ = vdom.rebuild(); let renderered = dioxus_ssr::pre_render(&vdom); let base_path = base_path.unwrap_or("."); - let head = head.unwrap_or_default(); + let head = head.unwrap_or( + r#"Dioxus Application + + + "#, + ); format!( r#" - {title} - - - {head} diff --git a/packages/server/src/serve.rs b/packages/server/src/serve.rs new file mode 100644 index 000000000..7dc1eaaac --- /dev/null +++ b/packages/server/src/serve.rs @@ -0,0 +1,47 @@ +use dioxus_core::Component; + +#[derive(Clone)] +pub struct ServeConfig { + pub(crate) app: Component, + pub(crate) application_name: Option<&'static str>, + pub(crate) server_fn_route: Option<&'static str>, + pub(crate) base_path: Option<&'static str>, + pub(crate) head: Option<&'static str>, +} + +impl ServeConfig { + /// Create a new ServeConfig + pub fn new(app: Component) -> Self { + Self { + app, + application_name: None, + server_fn_route: None, + base_path: None, + head: None, + } + } + + /// Set the application name matching the name in the Dioxus.toml file used to build the application + pub fn application_name(mut self, application_name: &'static str) -> Self { + self.application_name = Some(application_name); + self + } + + /// Set the base route all server functions will be served under + pub fn server_fn_route(mut self, server_fn_route: &'static str) -> Self { + self.server_fn_route = Some(server_fn_route); + self + } + + /// Set the path the WASM application will be served under + pub fn base_path(mut self, base_path: &'static str) -> Self { + self.base_path = Some(base_path); + self + } + + /// Set the head content to be included in the HTML document served + pub fn head(mut self, head: &'static str) -> Self { + self.head = Some(head); + self + } +}