From 71ddd50963b9aae55386ba64936d217a9bf6b5b7 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Sat, 1 Apr 2023 17:00:09 -0500 Subject: [PATCH] provide a nicer builder API --- .../examples/axum-hello-world/src/main.rs | 6 +-- .../server/examples/axum-router/src/main.rs | 17 ++++--- .../examples/salvo-hello-world/src/main.rs | 5 +- .../examples/warp-hello-world/src/main.rs | 4 +- packages/server/src/render.rs | 28 ++--------- packages/server/src/serve.rs | 47 ++++++++++++++++++- 6 files changed, 64 insertions(+), 43 deletions(-) diff --git a/packages/server/examples/axum-hello-world/src/main.rs b/packages/server/examples/axum-hello-world/src/main.rs index b4bc76390..fc07c6fbb 100644 --- a/packages/server/examples/axum-hello-world/src/main.rs +++ b/packages/server/examples/axum-hello-world/src/main.rs @@ -23,11 +23,7 @@ fn main() { axum::Server::bind(&addr) .serve( axum::Router::new() - .serve_dioxus_application( - ServeConfig::new(app, ()).head(r#"Hello World!"#), - None, - ) - .with_state(SSRState::default()) + .serve_dioxus_application("", ServeConfigBuilder::new(app, ())) .into_make_service(), ) .await diff --git a/packages/server/examples/axum-router/src/main.rs b/packages/server/examples/axum-router/src/main.rs index 0ae68d50e..53193859c 100644 --- a/packages/server/examples/axum-router/src/main.rs +++ b/packages/server/examples/axum-router/src/main.rs @@ -41,12 +41,15 @@ fn main() { // If the path is unknown, render the application .fallback( move |uri: http::uri::Uri, State(ssr_state): State| { - let rendered = ssr_state.render(&ServeConfig::new( - App, - AppProps { - route: Some(format!("http://{addr}{uri}")), - }, - )); + let rendered = ssr_state.render( + &ServeConfigBuilder::new( + App, + AppProps { + route: Some(format!("http://{addr}{uri}")), + }, + ) + .build(), + ); async move { axum::body::Full::from(rendered) } }, ) @@ -79,7 +82,7 @@ fn App(cx: Scope) -> Element { for _ in 0..100 { tr { for _ in 0..100 { - td { "hello world??" } + td { "hello world!" } } } } diff --git a/packages/server/examples/salvo-hello-world/src/main.rs b/packages/server/examples/salvo-hello-world/src/main.rs index cd8b80c51..f13668c5e 100644 --- a/packages/server/examples/salvo-hello-world/src/main.rs +++ b/packages/server/examples/salvo-hello-world/src/main.rs @@ -20,9 +20,8 @@ fn main() { tokio::runtime::Runtime::new() .unwrap() .block_on(async move { - let router = Router::new().serve_dioxus_application( - ServeConfig::new(app, ()).head(r#"Hello World!"#), - ); + let router = + Router::new().serve_dioxus_application("", ServeConfigBuilder::new(app, ())); Server::new(TcpListener::bind("127.0.0.1:8080")) .serve(router) .await; diff --git a/packages/server/examples/warp-hello-world/src/main.rs b/packages/server/examples/warp-hello-world/src/main.rs index 2bac390e2..87f91782a 100644 --- a/packages/server/examples/warp-hello-world/src/main.rs +++ b/packages/server/examples/warp-hello-world/src/main.rs @@ -19,9 +19,7 @@ fn main() { tokio::runtime::Runtime::new() .unwrap() .block_on(async move { - let routes = serve_dioxus_application( - ServeConfig::new(app, ()).head(r#"Hello World!"#), - ); + let routes = serve_dioxus_application("", ServeConfigBuilder::new(app, ())); warp::serve(routes).run(([127, 0, 0, 1], 8080)).await; }); } diff --git a/packages/server/src/render.rs b/packages/server/src/render.rs index 872a6cffc..cb752eaed 100644 --- a/packages/server/src/render.rs +++ b/packages/server/src/render.rs @@ -16,38 +16,20 @@ fn dioxus_ssr_html(cfg: &ServeConfig

, renderer: &mut Rend .. } = cfg; - let application_name = application_name.unwrap_or("dioxus"); let mut vdom = VirtualDom::new_with_props(*app, props.clone()); let _ = vdom.rebuild(); - let base_path = base_path.unwrap_or("."); let mut html = String::new(); - let result = match head { - Some(head) => { - write!( - &mut html, - r#" + let result = write!( + &mut html, + r#" {head} - - +

"# - ) - } - None => { - write!( - &mut html, - r#"Dioxus Application - - - - - "# - ) - } - }; + ); if let Err(err) = result { eprintln!("Failed to write to html: {}", err); diff --git a/packages/server/src/serve.rs b/packages/server/src/serve.rs index 65f0871ae..ed059f47f 100644 --- a/packages/server/src/serve.rs +++ b/packages/server/src/serve.rs @@ -1,15 +1,16 @@ use dioxus_core::Component; #[derive(Clone)] -pub struct ServeConfig { +pub struct ServeConfigBuilder { pub(crate) app: Component

, pub(crate) props: P, pub(crate) application_name: Option<&'static str>, pub(crate) base_path: Option<&'static str>, pub(crate) head: Option<&'static str>, + pub(crate) assets_path: Option<&'static str>, } -impl ServeConfig

{ +impl ServeConfigBuilder

{ /// Create a new ServeConfig pub fn new(app: Component

, props: P) -> Self { Self { @@ -18,6 +19,7 @@ impl ServeConfig

{ application_name: None, base_path: None, head: None, + assets_path: None, } } @@ -38,4 +40,45 @@ impl ServeConfig

{ self.head = Some(head); self } + + /// Set the path of the assets folder generated by the Dioxus CLI. (defaults to dist/assets) + pub fn assets_path(mut self, assets_path: &'static str) -> Self { + self.assets_path = Some(assets_path); + self + } + + /// Build the ServeConfig + pub fn build(self) -> ServeConfig

{ + let base_path = self.base_path.unwrap_or("."); + let application_name = self.application_name.unwrap_or("dioxus"); + ServeConfig { + app: self.app, + props: self.props, + application_name, + base_path, + head: self.head.map(String::from).unwrap_or(format!(r#"Dioxus Application + + + + + "#)), + assets_path: self.assets_path.unwrap_or("dist/assets"), + } + } +} + +#[derive(Clone)] +pub struct ServeConfig { + pub(crate) app: Component

, + pub(crate) props: P, + pub(crate) application_name: &'static str, + pub(crate) base_path: &'static str, + pub(crate) head: String, + pub(crate) assets_path: &'static str, +} + +impl From> for ServeConfig

{ + fn from(builder: ServeConfigBuilder

) -> Self { + builder.build() + } }