diff --git a/Cargo.toml b/Cargo.toml index 720b9e274..a5beb4aeb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,9 +25,10 @@ members = [ "packages/server", "packages/server/server-macro", "packages/server/examples/axum-hello-world", + "packages/server/examples/axum-router", + "packages/server/examples/axum-desktop", "packages/server/examples/salvo-hello-world", "packages/server/examples/warp-hello-world", - "packages/server/examples/axum-router", "docs/guide", ] diff --git a/packages/server/examples/axum-desktop/.gitignore b/packages/server/examples/axum-desktop/.gitignore new file mode 100644 index 000000000..6047329c6 --- /dev/null +++ b/packages/server/examples/axum-desktop/.gitignore @@ -0,0 +1,2 @@ +dist +target \ No newline at end of file diff --git a/packages/server/examples/axum-desktop/Cargo.toml b/packages/server/examples/axum-desktop/Cargo.toml new file mode 100644 index 000000000..817202bd1 --- /dev/null +++ b/packages/server/examples/axum-desktop/Cargo.toml @@ -0,0 +1,31 @@ +[package] +name = "axum-desktop" +version = "0.1.0" +edition = "2021" +publish = false + +[lib] + +[dependencies] +dioxus-desktop = { path = "../../../desktop", optional = true } +dioxus = { path = "../../../dioxus" } +dioxus-router = { path = "../../../router" } +dioxus-server = { path = "../../" } +axum = { version = "0.6.12", optional = true } +tokio = { version = "1.27.0", features = ["full"], optional = true } +serde = "1.0.159" + +[features] +default = [] +ssr = ["axum", "tokio", "dioxus-server/axum"] +desktop = ["dioxus-desktop"] + +[[bin]] +name = "client" +path = "src/client.rs" +required-features = ["desktop"] + +[[bin]] +name = "server" +path = "src/server.rs" +required-features = ["ssr"] diff --git a/packages/server/examples/axum-desktop/src/client.rs b/packages/server/examples/axum-desktop/src/client.rs new file mode 100644 index 000000000..588cf2003 --- /dev/null +++ b/packages/server/examples/axum-desktop/src/client.rs @@ -0,0 +1,13 @@ +// Run with: +// ```bash +// cargo run --bin client --features="desktop" +// ``` + +use axum_desktop::*; +use dioxus_server::prelude::server_fn::set_server_url; + +fn main() { + // Set the url of the server where server functions are hosted. + set_server_url("http://localhost:8080"); + dioxus_desktop::launch(app) +} diff --git a/packages/server/examples/axum-desktop/src/lib.rs b/packages/server/examples/axum-desktop/src/lib.rs new file mode 100644 index 000000000..3bae1dcde --- /dev/null +++ b/packages/server/examples/axum-desktop/src/lib.rs @@ -0,0 +1,40 @@ +#![allow(non_snake_case)] +use dioxus::prelude::*; +use dioxus_server::prelude::*; + +pub fn app(cx: Scope) -> Element { + let mut count = use_state(cx, || 0); + let text = use_state(cx, || "...".to_string()); + + cx.render(rsx! { + h1 { "High-Five counter: {count}" } + button { onclick: move |_| count += 1, "Up high!" } + button { onclick: move |_| count -= 1, "Down low!" } + button { + onclick: move |_| { + to_owned![text]; + async move { + if let Ok(data) = get_server_data().await { + println!("Client received: {}", data); + text.set(data.clone()); + post_server_data(data).await.unwrap(); + } + } + }, + "Run a server function" + } + "Server said: {text}" + }) +} + +#[server(PostServerData)] +async fn post_server_data(data: String) -> Result<(), ServerFnError> { + println!("Server received: {}", data); + + Ok(()) +} + +#[server(GetServerData)] +async fn get_server_data() -> Result { + Ok("Hello from the server!".to_string()) +} diff --git a/packages/server/examples/axum-desktop/src/server.rs b/packages/server/examples/axum-desktop/src/server.rs new file mode 100644 index 000000000..ff607dbcd --- /dev/null +++ b/packages/server/examples/axum-desktop/src/server.rs @@ -0,0 +1,23 @@ +// Run with: +// ```bash +// cargo run --bin server --features="ssr" +// ``` + +use axum_desktop::*; +use dioxus_server::prelude::*; + +#[tokio::main] +async fn main() { + PostServerData::register().unwrap(); + GetServerData::register().unwrap(); + + let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 8080)); + axum::Server::bind(&addr) + .serve( + axum::Router::new() + .register_server_fns("") + .into_make_service(), + ) + .await + .unwrap(); +} diff --git a/packages/server/examples/axum-hello-world/Cargo.toml b/packages/server/examples/axum-hello-world/Cargo.toml index 50d5e05b3..fd1f051d1 100644 --- a/packages/server/examples/axum-hello-world/Cargo.toml +++ b/packages/server/examples/axum-hello-world/Cargo.toml @@ -9,7 +9,6 @@ publish = false [dependencies] dioxus-web = { path = "../../../web", features=["hydrate"], optional = true } dioxus = { path = "../../../dioxus" } -dioxus-router = { path = "../../../router" } dioxus-server = { path = "../../" } axum = { version = "0.6.12", optional = true } tokio = { version = "1.27.0", features = ["full"], optional = true } @@ -18,4 +17,4 @@ serde = "1.0.159" [features] default = ["web"] ssr = ["axum", "tokio", "dioxus-server/axum"] -web = ["dioxus-web", "dioxus-router/web"] +web = ["dioxus-web"]