mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-27 14:40:44 +00:00
provide a nicer builder API
This commit is contained in:
parent
5ffdb4dbed
commit
71ddd50963
6 changed files with 64 additions and 43 deletions
|
@ -23,11 +23,7 @@ fn main() {
|
||||||
axum::Server::bind(&addr)
|
axum::Server::bind(&addr)
|
||||||
.serve(
|
.serve(
|
||||||
axum::Router::new()
|
axum::Router::new()
|
||||||
.serve_dioxus_application(
|
.serve_dioxus_application("", ServeConfigBuilder::new(app, ()))
|
||||||
ServeConfig::new(app, ()).head(r#"<title>Hello World!</title>"#),
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.with_state(SSRState::default())
|
|
||||||
.into_make_service(),
|
.into_make_service(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
|
|
@ -41,12 +41,15 @@ fn main() {
|
||||||
// If the path is unknown, render the application
|
// If the path is unknown, render the application
|
||||||
.fallback(
|
.fallback(
|
||||||
move |uri: http::uri::Uri, State(ssr_state): State<SSRState>| {
|
move |uri: http::uri::Uri, State(ssr_state): State<SSRState>| {
|
||||||
let rendered = ssr_state.render(&ServeConfig::new(
|
let rendered = ssr_state.render(
|
||||||
App,
|
&ServeConfigBuilder::new(
|
||||||
AppProps {
|
App,
|
||||||
route: Some(format!("http://{addr}{uri}")),
|
AppProps {
|
||||||
},
|
route: Some(format!("http://{addr}{uri}")),
|
||||||
));
|
},
|
||||||
|
)
|
||||||
|
.build(),
|
||||||
|
);
|
||||||
async move { axum::body::Full::from(rendered) }
|
async move { axum::body::Full::from(rendered) }
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
@ -79,7 +82,7 @@ fn App(cx: Scope<AppProps>) -> Element {
|
||||||
for _ in 0..100 {
|
for _ in 0..100 {
|
||||||
tr {
|
tr {
|
||||||
for _ in 0..100 {
|
for _ in 0..100 {
|
||||||
td { "hello world??" }
|
td { "hello world!" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,9 +20,8 @@ fn main() {
|
||||||
tokio::runtime::Runtime::new()
|
tokio::runtime::Runtime::new()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.block_on(async move {
|
.block_on(async move {
|
||||||
let router = Router::new().serve_dioxus_application(
|
let router =
|
||||||
ServeConfig::new(app, ()).head(r#"<title>Hello World!</title>"#),
|
Router::new().serve_dioxus_application("", ServeConfigBuilder::new(app, ()));
|
||||||
);
|
|
||||||
Server::new(TcpListener::bind("127.0.0.1:8080"))
|
Server::new(TcpListener::bind("127.0.0.1:8080"))
|
||||||
.serve(router)
|
.serve(router)
|
||||||
.await;
|
.await;
|
||||||
|
|
|
@ -19,9 +19,7 @@ fn main() {
|
||||||
tokio::runtime::Runtime::new()
|
tokio::runtime::Runtime::new()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.block_on(async move {
|
.block_on(async move {
|
||||||
let routes = serve_dioxus_application(
|
let routes = serve_dioxus_application("", ServeConfigBuilder::new(app, ()));
|
||||||
ServeConfig::new(app, ()).head(r#"<title>Hello World!</title>"#),
|
|
||||||
);
|
|
||||||
warp::serve(routes).run(([127, 0, 0, 1], 8080)).await;
|
warp::serve(routes).run(([127, 0, 0, 1], 8080)).await;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,38 +16,20 @@ fn dioxus_ssr_html<P: 'static + Clone>(cfg: &ServeConfig<P>, renderer: &mut Rend
|
||||||
..
|
..
|
||||||
} = cfg;
|
} = cfg;
|
||||||
|
|
||||||
let application_name = application_name.unwrap_or("dioxus");
|
|
||||||
let mut vdom = VirtualDom::new_with_props(*app, props.clone());
|
let mut vdom = VirtualDom::new_with_props(*app, props.clone());
|
||||||
let _ = vdom.rebuild();
|
let _ = vdom.rebuild();
|
||||||
let base_path = base_path.unwrap_or(".");
|
|
||||||
|
|
||||||
let mut html = String::new();
|
let mut html = String::new();
|
||||||
|
|
||||||
let result = match head {
|
let result = write!(
|
||||||
Some(head) => {
|
&mut html,
|
||||||
write!(
|
r#"
|
||||||
&mut html,
|
|
||||||
r#"
|
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>{head}
|
<head>{head}
|
||||||
</head>
|
</head><body>
|
||||||
<body>
|
|
||||||
<div id="main">"#
|
<div id="main">"#
|
||||||
)
|
);
|
||||||
}
|
|
||||||
None => {
|
|
||||||
write!(
|
|
||||||
&mut html,
|
|
||||||
r#"<title>Dioxus Application</title>
|
|
||||||
<link rel="preload" href="/{base_path}/assets/dioxus/{application_name}_bg.wasm" as="fetch" type="application/wasm" crossorigin="" />
|
|
||||||
<link rel="modulepreload" href="/{base_path}/assets/dioxus/{application_name}.js" />
|
|
||||||
<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" />"#
|
|
||||||
)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Err(err) = result {
|
if let Err(err) = result {
|
||||||
eprintln!("Failed to write to html: {}", err);
|
eprintln!("Failed to write to html: {}", err);
|
||||||
|
|
|
@ -1,15 +1,16 @@
|
||||||
use dioxus_core::Component;
|
use dioxus_core::Component;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct ServeConfig<P: Clone> {
|
pub struct ServeConfigBuilder<P: Clone> {
|
||||||
pub(crate) app: Component<P>,
|
pub(crate) app: Component<P>,
|
||||||
pub(crate) props: P,
|
pub(crate) props: P,
|
||||||
pub(crate) application_name: Option<&'static str>,
|
pub(crate) application_name: Option<&'static str>,
|
||||||
pub(crate) base_path: Option<&'static str>,
|
pub(crate) base_path: Option<&'static str>,
|
||||||
pub(crate) head: Option<&'static str>,
|
pub(crate) head: Option<&'static str>,
|
||||||
|
pub(crate) assets_path: Option<&'static str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<P: Clone> ServeConfig<P> {
|
impl<P: Clone> ServeConfigBuilder<P> {
|
||||||
/// Create a new ServeConfig
|
/// Create a new ServeConfig
|
||||||
pub fn new(app: Component<P>, props: P) -> Self {
|
pub fn new(app: Component<P>, props: P) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
@ -18,6 +19,7 @@ impl<P: Clone> ServeConfig<P> {
|
||||||
application_name: None,
|
application_name: None,
|
||||||
base_path: None,
|
base_path: None,
|
||||||
head: None,
|
head: None,
|
||||||
|
assets_path: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,4 +40,45 @@ impl<P: Clone> ServeConfig<P> {
|
||||||
self.head = Some(head);
|
self.head = Some(head);
|
||||||
self
|
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<P> {
|
||||||
|
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#"<title>Dioxus Application</title>
|
||||||
|
<link rel="preload" href="/{base_path}/assets/dioxus/{application_name}_bg.wasm" as="fetch" type="application/wasm" crossorigin="" />
|
||||||
|
<link rel="modulepreload" href="/{base_path}/assets/dioxus/{application_name}.js" />
|
||||||
|
<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" />"#)),
|
||||||
|
assets_path: self.assets_path.unwrap_or("dist/assets"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct ServeConfig<P: Clone> {
|
||||||
|
pub(crate) app: Component<P>,
|
||||||
|
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<P: Clone> From<ServeConfigBuilder<P>> for ServeConfig<P> {
|
||||||
|
fn from(builder: ServeConfigBuilder<P>) -> Self {
|
||||||
|
builder.build()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue