create a serveconfig builder
This commit is contained in:
1 parent
1d395d572f
commit
1be48c4aa8
4 files changed
+74
-39
No files matched your search
@@ -17,12 +17,7 @@ fn main() {
|
|||||||
.serve(
|
.serve(
|
||||||
axum::Router::new()
|
axum::Router::new()
|
||||||
.serve_dioxus_application(
|
.serve_dioxus_application(
|
||||||
"Hello, world!",
|
ServeConfig::new(app).head(r#"<title>Hello World!</title>"#),
|
||||||
"hello-world",
|
|
||||||
None,
|
|
||||||
None,
|
|
||||||
"",
|
|
||||||
app,
|
|
||||||
)
|
)
|
||||||
.into_make_service(),
|
.into_make_service(),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -7,26 +7,18 @@ use axum::{
|
|||||||
routing::{get, post},
|
routing::{get, post},
|
||||||
Router,
|
Router,
|
||||||
};
|
};
|
||||||
use dioxus_core::Component;
|
|
||||||
use server_fn::{Payload, ServerFunctionRegistry};
|
use server_fn::{Payload, ServerFunctionRegistry};
|
||||||
use tokio::task::spawn_blocking;
|
use tokio::task::spawn_blocking;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
dioxus_ssr_html,
|
dioxus_ssr_html,
|
||||||
|
serve::ServeConfig,
|
||||||
server_fn::{DioxusServerContext, DioxusServerFnRegistry, ServerFnTraitObj},
|
server_fn::{DioxusServerContext, DioxusServerFnRegistry, ServerFnTraitObj},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub trait DioxusRouterExt {
|
pub trait DioxusRouterExt {
|
||||||
fn register_server_fns(self, server_fn_route: &'static str) -> Self;
|
fn register_server_fns(self, server_fn_route: &'static str) -> Self;
|
||||||
fn serve_dioxus_application(
|
fn serve_dioxus_application(self, cfg: ServeConfig) -> Self;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DioxusRouterExt for Router {
|
impl DioxusRouterExt for Router {
|
||||||
@@ -45,24 +37,16 @@ impl DioxusRouterExt for Router {
|
|||||||
router
|
router
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serve_dioxus_application(
|
fn serve_dioxus_application(self, cfg: ServeConfig) -> Self {
|
||||||
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 {
|
|
||||||
use tower_http::services::ServeDir;
|
use tower_http::services::ServeDir;
|
||||||
|
|
||||||
// Serve the dist folder and the index.html file
|
// Serve the dist folder and the index.html file
|
||||||
let serve_dir = ServeDir::new("dist");
|
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)
|
.nest_service("/", serve_dir)
|
||||||
.fallback_service(get(move || {
|
.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) }
|
async move { Full::from(rendered) }
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|||||||
+21
-12
@@ -2,38 +2,47 @@
|
|||||||
use dioxus_core::prelude::*;
|
use dioxus_core::prelude::*;
|
||||||
|
|
||||||
mod adapters;
|
mod adapters;
|
||||||
|
mod serve;
|
||||||
mod server_fn;
|
mod server_fn;
|
||||||
|
|
||||||
pub mod prelude {
|
pub mod prelude {
|
||||||
#[cfg(feature = "axum")]
|
#[cfg(feature = "axum")]
|
||||||
pub use crate::adapters::axum_adapter::*;
|
pub use crate::adapters::axum_adapter::*;
|
||||||
|
pub use crate::serve::ServeConfig;
|
||||||
pub use crate::server_fn::{DioxusServerContext, ServerFn};
|
pub use crate::server_fn::{DioxusServerContext, ServerFn};
|
||||||
pub use server_fn::{self, ServerFn as _, ServerFnError};
|
pub use server_fn::{self, ServerFn as _, ServerFnError};
|
||||||
pub use server_macro::*;
|
pub use server_macro::*;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "ssr")]
|
#[cfg(feature = "ssr")]
|
||||||
fn dioxus_ssr_html(
|
fn dioxus_ssr_html(cfg: serve::ServeConfig) -> String {
|
||||||
title: &str,
|
use prelude::ServeConfig;
|
||||||
application_name: &str,
|
|
||||||
base_path: Option<&str>,
|
let ServeConfig {
|
||||||
head: Option<&str>,
|
app,
|
||||||
app: Component,
|
application_name,
|
||||||
) -> String {
|
base_path,
|
||||||
|
head,
|
||||||
|
..
|
||||||
|
} = cfg;
|
||||||
|
|
||||||
|
let application_name = application_name.unwrap_or("dioxus");
|
||||||
|
|
||||||
let mut vdom = VirtualDom::new(app);
|
let mut vdom = VirtualDom::new(app);
|
||||||
let _ = vdom.rebuild();
|
let _ = vdom.rebuild();
|
||||||
let renderered = dioxus_ssr::pre_render(&vdom);
|
let renderered = dioxus_ssr::pre_render(&vdom);
|
||||||
let base_path = base_path.unwrap_or(".");
|
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!(
|
format!(
|
||||||
r#"
|
r#"
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<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}
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user