allow server functions to read request and modify responce

This commit is contained in:
Evan Almloff committed 2023-04-29 17:04:54 -05:00
1 parent 581a4648de
commit 7ff5d356d5
14 files changed
+387 -159

No files matched your search

@@ -18,3 +18,10 @@ serde = "1.0.159"
default = ["web"]
ssr = ["axum", "tokio", "dioxus-server/axum"]
web = ["dioxus-web"]
[profile.release]
lto = true
panic = "abort"
opt-level = 3
strip = true
codegen-units = 1
@@ -56,11 +56,12 @@ fn app(cx: Scope<AppProps>) -> Element {
button {
onclick: move |_| {
to_owned![text];
let sc = cx.sc();
async move {
if let Ok(data) = get_server_data().await {
println!("Client received: {}", data);
text.set(data.clone());
post_server_data(data).await.unwrap();
post_server_data(sc, data).await.unwrap();
}
}
},
@@ -71,8 +72,12 @@ fn app(cx: Scope<AppProps>) -> Element {
}
#[server(PostServerData)]
async fn post_server_data(data: String) -> Result<(), ServerFnError> {
async fn post_server_data(cx: DioxusServerContext, data: String) -> Result<(), ServerFnError> {
// The server context contains information about the current request and allows you to modify the response.
cx.responce_headers_mut()
.insert("Set-Cookie", "foo=bar".parse().unwrap());
println!("Server received: {}", data);
println!("Request parts are {:?}", cx.request_parts());
Ok(())
}
@@ -21,3 +21,10 @@ http = { version = "0.2.9", optional = true }
default = ["web"]
ssr = ["axum", "tokio", "dioxus-server/axum", "tower-http", "http"]
web = ["dioxus-web", "dioxus-router/web"]
[profile.release]
lto = true
panic = "abort"
opt-level = 3
strip = true
codegen-units = 1
@@ -18,3 +18,10 @@ salvo = { version = "0.37.9", optional = true }
default = ["web"]
ssr = ["salvo", "tokio", "dioxus-server/salvo"]
web = ["dioxus-web"]
[profile.release]
lto = true
panic = "abort"
opt-level = 3
strip = true
codegen-units = 1
@@ -44,6 +44,7 @@ struct AppProps {
fn app(cx: Scope<AppProps>) -> Element {
let mut count = use_state(cx, || cx.props.count);
let text = use_state(cx, || "...".to_string());
let server_context = cx.sc();
cx.render(rsx! {
h1 { "High-Five counter: {count}" }
@@ -51,12 +52,12 @@ fn app(cx: Scope<AppProps>) -> Element {
button { onclick: move |_| count -= 1, "Down low!" }
button {
onclick: move |_| {
to_owned![text];
to_owned![text, server_context];
async move {
if let Ok(data) = get_server_data().await {
println!("Client received: {}", data);
text.set(data.clone());
post_server_data(data).await.unwrap();
post_server_data(server_context, data).await.unwrap();
}
}
},
@@ -67,8 +68,12 @@ fn app(cx: Scope<AppProps>) -> Element {
}
#[server(PostServerData)]
async fn post_server_data(data: String) -> Result<(), ServerFnError> {
async fn post_server_data(cx: DioxusServerContext, data: String) -> Result<(), ServerFnError> {
// The server context contains information about the current request and allows you to modify the response.
cx.responce_headers_mut()
.insert("Set-Cookie", "foo=bar".parse().unwrap());
println!("Server received: {}", data);
println!("Request parts are {:?}", cx.request_parts());
Ok(())
}
@@ -18,3 +18,10 @@ warp = { version = "0.3.3", optional = true }
default = ["web"]
ssr = ["warp", "tokio", "dioxus-server/warp"]
web = ["dioxus-web"]
[profile.release]
lto = true
panic = "abort"
opt-level = 3
strip = true
codegen-units = 1
@@ -41,6 +41,7 @@ struct AppProps {
fn app(cx: Scope<AppProps>) -> Element {
let mut count = use_state(cx, || cx.props.count);
let text = use_state(cx, || "...".to_string());
let server_context = cx.sc();
cx.render(rsx! {
h1 { "High-Five counter: {count}" }
@@ -48,12 +49,12 @@ fn app(cx: Scope<AppProps>) -> Element {
button { onclick: move |_| count -= 1, "Down low!" }
button {
onclick: move |_| {
to_owned![text];
to_owned![text, server_context];
async move {
if let Ok(data) = get_server_data().await {
println!("Client received: {}", data);
text.set(data.clone());
post_server_data(data).await.unwrap();
post_server_data(server_context, data).await.unwrap();
}
}
},
@@ -64,8 +65,12 @@ fn app(cx: Scope<AppProps>) -> Element {
}
#[server(PostServerData)]
async fn post_server_data(data: String) -> Result<(), ServerFnError> {
async fn post_server_data(cx: DioxusServerContext, data: String) -> Result<(), ServerFnError> {
// The server context contains information about the current request and allows you to modify the response.
cx.responce_headers_mut()
.insert("Set-Cookie", "foo=bar".parse().unwrap());
println!("Server received: {}", data);
println!("Request parts are {:?}", cx.request_parts());
Ok(())
}