server.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. mod basic {
  2. // ANCHOR: basic
  3. #![allow(non_snake_case)]
  4. use dioxus::prelude::*;
  5. use dioxus_server::prelude::*;
  6. #[tokio::main]
  7. async fn main() {
  8. let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 8080));
  9. axum::Server::bind(&addr)
  10. .serve(
  11. axum::Router::new()
  12. .serve_dioxus_application("", ServeConfigBuilder::new(app, ()))
  13. .into_make_service(),
  14. )
  15. .await
  16. .unwrap();
  17. }
  18. fn app(cx: Scope) -> Element {
  19. let mut count = use_state(cx, || 0);
  20. cx.render(rsx! {
  21. h1 { "High-Five counter: {count}" }
  22. button { onclick: move |_| count += 1, "Up high!" }
  23. button { onclick: move |_| count -= 1, "Down low!" }
  24. })
  25. }
  26. // ANCHOR_END: basic
  27. }
  28. mod hydration {
  29. // ANCHOR: hydration
  30. #![allow(non_snake_case)]
  31. use dioxus::prelude::*;
  32. use dioxus_server::prelude::*;
  33. fn main() {
  34. #[cfg(feature = "web")]
  35. dioxus_web::launch_cfg(app, dioxus_web::Config::new().hydrate(true));
  36. #[cfg(feature = "ssr")]
  37. {
  38. tokio::runtime::Runtime::new()
  39. .unwrap()
  40. .block_on(async move {
  41. let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 8080));
  42. axum::Server::bind(&addr)
  43. .serve(
  44. axum::Router::new()
  45. .serve_dioxus_application("", ServeConfigBuilder::new(app, ()))
  46. .into_make_service(),
  47. )
  48. .await
  49. .unwrap();
  50. });
  51. }
  52. }
  53. fn app(cx: Scope) -> Element {
  54. let mut count = use_state(cx, || 0);
  55. cx.render(rsx! {
  56. h1 { "High-Five counter: {count}" }
  57. button { onclick: move |_| count += 1, "Up high!" }
  58. button { onclick: move |_| count -= 1, "Down low!" }
  59. })
  60. }
  61. // ANCHOR_END: hydration
  62. }
  63. mod server_function {
  64. // ANCHOR: server_function
  65. #![allow(non_snake_case)]
  66. use dioxus::prelude::*;
  67. use dioxus_server::prelude::*;
  68. fn main() {
  69. #[cfg(feature = "web")]
  70. dioxus_web::launch_cfg(app, dioxus_web::Config::new().hydrate(true));
  71. #[cfg(feature = "ssr")]
  72. {
  73. // Register the server function before starting the server
  74. DoubleServer::register().unwrap();
  75. tokio::runtime::Runtime::new()
  76. .unwrap()
  77. .block_on(async move {
  78. let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 8080));
  79. axum::Server::bind(&addr)
  80. .serve(
  81. axum::Router::new()
  82. // Serve Dioxus application automatically recognizes server functions and adds them to the API
  83. .serve_dioxus_application("", ServeConfigBuilder::new(app, ()))
  84. .into_make_service(),
  85. )
  86. .await
  87. .unwrap();
  88. });
  89. }
  90. }
  91. fn app(cx: Scope) -> Element {
  92. let mut count = use_state(cx, || 0);
  93. cx.render(rsx! {
  94. h1 { "High-Five counter: {count}" }
  95. button { onclick: move |_| count += 1, "Up high!" }
  96. button { onclick: move |_| count -= 1, "Down low!" }
  97. button {
  98. onclick: move |_| {
  99. to_owned![count];
  100. async move {
  101. // Call the server function just like a local async function
  102. if let Ok(new_count) = double_server(*count.current()).await {
  103. count.set(new_count);
  104. }
  105. }
  106. },
  107. "Double"
  108. }
  109. })
  110. }
  111. #[server(DoubleServer)]
  112. async fn double_server(number: u32) -> Result<u32, ServerFnError> {
  113. // Perform some expensive computation or access a database on the server
  114. tokio::time::sleep(std::time::Duration::from_secs(1)).await;
  115. let result = number * 2;
  116. println!("server calculated {result}");
  117. Ok(result)
  118. }
  119. // ANCHOR_END: server_function
  120. }