add desktop server functions example

This commit is contained in:
Evan Almloff committed 2023-04-03 13:34:34 -05:00
1 parent 7ae45272d1
commit 0e445c9f59
7 files changed
+112 -3

No files matched your search

+2 -1
View File
@@ -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",
]
@@ -0,0 +1,2 @@
dist
target
@@ -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"]
@@ -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)
}
@@ -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<String, ServerFnError> {
Ok("Hello from the server!".to_string())
}
@@ -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();
}
@@ -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"]