add salvo intigration

This commit is contained in:
Evan Almloff committed 2023-03-30 19:42:53 -05:00
1 parent f618da7311
commit 83d513ef36
7 files changed
+238 -19

No files matched your search

@@ -1,5 +1,5 @@
[package]
name = "hello-world"
name = "axum-hello-world"
version = "0.1.0"
edition = "2021"
@@ -1,5 +1,5 @@
//! Run with:
//!
//!
//! ```sh
//! dioxus build --features web
//! cargo run --features ssr
@@ -24,7 +24,7 @@ fn main() {
.serve(
axum::Router::new()
.serve_dioxus_application(
ServeConfig::new(app).head(r#"<title>Hello World!</title>"#),
ServeConfig::new(app, ()).head(r#"<title>Hello World!</title>"#),
)
.into_make_service(),
)
@@ -0,0 +1,2 @@
dist
target
@@ -0,0 +1,20 @@
[package]
name = "salvo-hello-world"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
dioxus-web = { path = "../../../web", features=["hydrate"], optional = true }
dioxus = { path = "../../../dioxus" }
dioxus-server = { path = "../../" }
tokio = { version = "1.27.0", features = ["full"], optional = true }
serde = "1.0.159"
tracing-subscriber = "0.3.16"
tracing = "0.1.37"
salvo = { version = "0.37.9", optional = true }
[features]
ssr = ["salvo", "tokio", "dioxus-server/ssr", "dioxus-server/salvo"]
web = ["dioxus-web"]
@@ -0,0 +1,68 @@
//! Run with:
//!
//! ```sh
//! dioxus build --features web
//! cargo run --features ssr
//! ```
#![allow(non_snake_case)]
use dioxus::prelude::*;
use dioxus_server::prelude::*;
fn main() {
#[cfg(feature = "web")]
dioxus_web::launch_cfg(app, dioxus_web::Config::new().hydrate(true));
#[cfg(feature = "ssr")]
{
use salvo::prelude::*;
PostServerData::register().unwrap();
GetServerData::register().unwrap();
tokio::runtime::Runtime::new()
.unwrap()
.block_on(async move {
let router = Router::new().serve_dioxus_application(
ServeConfig::new(app, ()).head(r#"<title>Hello World!</title>"#),
);
Server::new(TcpListener::bind("127.0.0.1:8080"))
.serve(router)
.await;
});
}
}
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())
}