Browse Source

create a serveconfig builder

Evan Almloff 2 years ago
parent
commit
1be48c4aa8

+ 1 - 6
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#"<title>Hello World!</title>"#),
                             )
                             .into_make_service(),
                     )

+ 5 - 21
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) }
             }))
     }

+ 21 - 12
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#"<title>Dioxus Application</title>
+  <meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
+  <meta name="viewport" content="width=device-width, initial-scale=1" />
+  <meta charset="UTF-8" />"#,
+    );
     format!(
         r#"
     <!DOCTYPE html>
 <html>
 <head>
-  <title>{title}</title>
-  <meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
-  <meta name="viewport" content="width=device-width, initial-scale=1" />
-  <meta charset="UTF-8" />
   {head}
 </head>
 <body>

+ 47 - 0
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
+    }
+}