create server launch macro

This commit is contained in:
Evan Almloff committed 2023-06-23 18:23:03 -07:00
1 parent f9161f0a9b
commit 99674fcf94
20 files changed
+359 -334

No files matched your search

@@ -2,76 +2,37 @@
//!
//! ```sh
//! dioxus build --features web
//! cargo run --features ssr --no-default-features
//! cargo run --features ssr
//! ```
#![allow(non_snake_case)]
use dioxus::prelude::*;
use dioxus_fullstack::prelude::*;
use dioxus_router::prelude::*;
use serde::{Deserialize, Serialize};
fn main() {
#[cfg(feature = "web")]
dioxus_web::launch_with_props(
Router,
Default::default(),
dioxus_web::Config::new().hydrate(true),
);
#[cfg(feature = "ssr")]
{
// Start hot reloading
hot_reload_init!(dioxus_hot_reload::Config::new().with_rebuild_callback(|| {
execute::shell("dioxus build --features web")
.spawn()
.unwrap()
.wait()
.unwrap();
execute::shell("cargo run --features ssr --no-default-features")
.spawn()
.unwrap();
true
}));
tokio::runtime::Runtime::new()
.unwrap()
.block_on(async move {
let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 8080));
axum::Server::bind(&addr)
.serve(
axum::Router::new()
.serve_dioxus_application(
"",
ServeConfigBuilder::new_with_router(
dioxus_fullstack::prelude::FullstackRouterConfig::<Route>::default()).incremental(IncrementalRendererConfig::default())
.build(),
)
.into_make_service(),
)
.await
.unwrap();
});
}
launch_router!(@[([127, 0, 0, 1], 8080)], Route, {
incremental,
});
}
#[derive(Clone, Routable, Serialize, Deserialize, Debug, PartialEq)]
#[derive(Clone, Routable, Debug, PartialEq)]
enum Route {
#[route("/")]
Home {},
#[route("/blog")]
Blog {},
#[route("/blog/:id")]
Blog { id: i32 },
}
#[inline_props]
fn Blog(cx: Scope) -> Element {
fn Blog(cx: Scope, id: i32) -> Element {
render! {
Link { target: Route::Home {}, "Go to counter" }
table {
tbody {
for _ in 0..100 {
for _ in 0..*id {
tr {
for _ in 0..100 {
for _ in 0..*id {
td { "hello world!" }
}
}
@@ -87,7 +48,12 @@ fn Home(cx: Scope) -> Element {
let text = use_state(cx, || "...".to_string());
cx.render(rsx! {
Link { target: Route::Blog {}, "Go to blog" }
Link {
target: Route::Blog {
id: *count.get()
},
"Go to blog"
}
div {
h1 { "High-Five counter: {count}" }
button { onclick: move |_| count += 1, "Up high!" }
@@ -103,7 +69,7 @@ fn Home(cx: Scope) -> Element {
}
}
},
"Run a server function"
"Run server function!"
}
"Server said: {text}"
}