server_function.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #![allow(non_snake_case, unused)]
  2. use dioxus::prelude::*;
  3. use dioxus_fullstack::prelude::*;
  4. fn main() {
  5. #[cfg(feature = "web")]
  6. dioxus_web::launch_with_props(
  7. app,
  8. // Get the root props from the document
  9. get_root_props_from_document().unwrap_or_default(),
  10. dioxus_web::Config::new().hydrate(true),
  11. );
  12. #[cfg(feature = "ssr")]
  13. {
  14. use axum::extract::Path;
  15. use axum::extract::State;
  16. use axum::routing::get;
  17. // Register the server function before starting the server
  18. DoubleServer::register().unwrap();
  19. tokio::runtime::Runtime::new()
  20. .unwrap()
  21. .block_on(async move {
  22. let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 8080));
  23. axum::Server::bind(&addr)
  24. .serve(
  25. axum::Router::new()
  26. // Serve the dist folder with the static javascript and WASM files created by the dixous CLI
  27. .serve_static_assets("./dist")
  28. // Register server functions
  29. .register_server_fns("")
  30. // Connect to the hot reload server in debug mode
  31. .connect_hot_reload()
  32. // Render the application. This will serialize the root props (the intial count) into the HTML
  33. .route(
  34. "/",
  35. get(move |Path(intial_count): Path<usize>, State(ssr_state): State<SSRState>| async move { axum::body::Full::from(
  36. ssr_state.render(
  37. &ServeConfigBuilder::new(
  38. app,
  39. intial_count,
  40. )
  41. .build(),
  42. )
  43. )}),
  44. )
  45. // Render the application with a different intial count
  46. .route(
  47. "/:initial_count",
  48. get(move |Path(intial_count): Path<usize>, State(ssr_state): State<SSRState>| async move { axum::body::Full::from(
  49. ssr_state.render(
  50. &ServeConfigBuilder::new(
  51. app,
  52. intial_count,
  53. )
  54. .build(),
  55. )
  56. )}),
  57. )
  58. .with_state(SSRState::default())
  59. .into_make_service(),
  60. )
  61. .await
  62. .unwrap();
  63. });
  64. }
  65. }
  66. fn app(cx: Scope<usize>) -> Element {
  67. let mut count = use_state(cx, || *cx.props);
  68. cx.render(rsx! {
  69. h1 { "High-Five counter: {count}" }
  70. button { onclick: move |_| count += 1, "Up high!" }
  71. button { onclick: move |_| count -= 1, "Down low!" }
  72. button {
  73. onclick: move |_| {
  74. to_owned![count];
  75. async move {
  76. // Call the server function just like a local async function
  77. if let Ok(new_count) = double_server(*count.current()).await {
  78. count.set(new_count);
  79. }
  80. }
  81. },
  82. "Double"
  83. }
  84. })
  85. }
  86. #[server(DoubleServer)]
  87. async fn double_server(number: usize) -> Result<usize, ServerFnError> {
  88. // Perform some expensive computation or access a database on the server
  89. tokio::time::sleep(std::time::Duration::from_secs(1)).await;
  90. let result = number * 2;
  91. println!("server calculated {result}");
  92. Ok(result)
  93. }