Re-export the axum feature flag under "server" (#2786)

* re-export the axum feature flag under "server"
* remove the axum prefix from all the fullstack examples
This commit is contained in:
Evan Almloff authored and GitHub committed 2024-08-06 21:35:04 +00:00
1 parent 37ea8ab906
commit eeccb741d3
24 files changed
+269 -283

No files matched your search

@@ -0,0 +1,4 @@
dist
target
static
.dioxus
@@ -0,0 +1,28 @@
[package]
name = "fullstack-desktop-example"
version = "0.1.0"
edition = "2021"
publish = false
[lib]
[dependencies]
dioxus = { workspace = true, features = ["launch", "fullstack"] }
axum = { workspace = true, optional = true }
tokio = { workspace = true, features = ["full"], optional = true }
serde = "1.0.159"
[features]
default = []
server = ["axum", "tokio", "dioxus/axum"]
desktop = ["dioxus/desktop"]
[[bin]]
name = "client"
path = "src/client.rs"
required-features = ["desktop"]
[[bin]]
name = "server"
path = "src/server.rs"
required-features = ["server"]
@@ -0,0 +1,15 @@
// Run with:
// ```bash
// cargo run --bin client --features desktop
// ```
use fullstack_desktop_example::*;
fn main() {
// Set the url of the server where server functions are hosted.
#[cfg(not(feature = "server"))]
dioxus::fullstack::prelude::server_fn::client::set_server_url("http://127.0.0.1:8080");
#[cfg(feature = "desktop")]
dioxus::prelude::launch_desktop(app)
}
@@ -0,0 +1,36 @@
#![allow(non_snake_case)]
use dioxus::prelude::*;
pub fn app() -> Element {
let mut count = use_signal(|| 0);
let mut text = use_signal(|| "...".to_string());
rsx! {
h1 { "High-Five counter: {count}" }
button { onclick: move |_| count += 1, "Up high!" }
button { onclick: move |_| count -= 1, "Down low!" }
button {
onclick: move |_| 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,69 @@
#![allow(non_snake_case)]
use dioxus::prelude::*;
/// Run with `cargo run --features desktop`
#[cfg(feature = "desktop")]
fn main() {
// Set the url of the server where server functions are hosted.
dioxus::fullstack::prelude::server_fn::client::set_server_url("http://127.0.0.1:8080");
// And then launch the app
dioxus::prelude::launch_desktop(app);
}
/// Run with `cargo run --features server`
#[cfg(all(feature = "server", not(feature = "desktop")))]
#[tokio::main]
async fn main() {
use server_fn::axum::register_explicit;
let listener = tokio::net::TcpListener::bind("127.0.0.01:8080")
.await
.unwrap();
register_explicit::<PostServerData>();
register_explicit::<GetServerData>();
axum::serve(
listener,
axum::Router::new()
.register_server_functions()
.into_make_service(),
)
.await
.unwrap();
}
pub fn app() -> Element {
let mut count = use_signal(|| 0);
let mut text = use_signal(|| "...".to_string());
rsx! {
h1 { "High-Five counter: {count}" }
button { onclick: move |_| count += 1, "Up high!" }
button { onclick: move |_| count -= 1, "Down low!" }
button {
onclick: move |_| 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,27 @@
// Run with:
// ```bash
// cargo run --bin server --features server
// ```
use dioxus::prelude::*;
use fullstack_desktop_example::*;
use server_fn::axum::register_explicit;
#[tokio::main]
async fn main() {
let listener = tokio::net::TcpListener::bind("127.0.0.01:8080")
.await
.unwrap();
register_explicit::<PostServerData>();
register_explicit::<GetServerData>();
axum::serve(
listener,
axum::Router::new()
.register_server_functions()
.into_make_service(),
)
.await
.unwrap();
}