62 lines
1.7 KiB
Rust
62 lines
1.7 KiB
Rust
//! Run with:
|
|
//!
|
|
//! ```sh
|
|
//! dx build --features web
|
|
//! cargo run --features ssr
|
|
//! ```
|
|
|
|
#![allow(non_snake_case, unused)]
|
|
use dioxus::prelude::*;
|
|
use dioxus_fullstack::{launch, prelude::*};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Props, PartialEq, Debug, Default, Serialize, Deserialize, Clone)]
|
|
struct AppProps {
|
|
count: i32,
|
|
}
|
|
|
|
fn app(cx: Scope<AppProps>) -> Element {
|
|
let mut count = use_state(cx, || cx.props.count);
|
|
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> {
|
|
let axum::extract::Host(host): axum::extract::Host = extract()?;
|
|
println!("Server received: {}", data);
|
|
println!("{:?}", host);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[server(GetServerData)]
|
|
async fn get_server_data() -> Result<String, ServerFnError> {
|
|
Ok("Hello from the server!".to_string())
|
|
}
|
|
|
|
fn main() {
|
|
launch!(@([127, 0, 0, 1], 8080), app, {
|
|
serve_cfg: ServeConfigBuilder::new(app, AppProps { count: 0 }),
|
|
});
|
|
}
|