server_context_state.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #![allow(non_snake_case, unused)]
  2. use dioxus::prelude::*;
  3. use dioxus_fullstack::prelude::*;
  4. #[cfg(feature = "ssr")]
  5. #[derive(Default, Clone)]
  6. struct ServerFunctionState {
  7. call_count: std::sync::Arc<std::sync::atomic::AtomicUsize>,
  8. }
  9. fn main() {
  10. #[cfg(feature = "web")]
  11. dioxus_web::launch_with_props(
  12. app,
  13. // Get the root props from the document
  14. get_root_props_from_document().unwrap_or_default(),
  15. dioxus_web::Config::new().hydrate(true),
  16. );
  17. #[cfg(feature = "ssr")]
  18. {
  19. use axum::body::Body;
  20. use axum::extract::Path;
  21. use axum::extract::State;
  22. use axum::http::Request;
  23. use axum::routing::get;
  24. use std::sync::Arc;
  25. // Register the server function before starting the server
  26. DoubleServer::register().unwrap();
  27. tokio::runtime::Runtime::new()
  28. .unwrap()
  29. .block_on(async move {
  30. let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 8080));
  31. axum::Server::bind(&addr)
  32. .serve(
  33. axum::Router::new()
  34. // Serve the dist folder with the static javascript and WASM files created by the dixous CLI
  35. .serve_static_assets("./dist")
  36. // Register server functions
  37. .register_server_fns_with_handler("", |func| {
  38. move |State(server_fn_state): State<ServerFunctionState>, req: Request<Body>| async move {
  39. let (parts, body) = req.into_parts();
  40. let parts: Arc<RequestParts> = Arc::new(parts.into());
  41. let mut server_context = DioxusServerContext::new(parts.clone());
  42. server_context.insert(server_fn_state);
  43. server_fn_handler(server_context, func.clone(), parts, body).await
  44. }
  45. })
  46. .with_state(ServerFunctionState::default())
  47. // Connect to the hot reload server in debug mode
  48. .connect_hot_reload()
  49. // Render the application. This will serialize the root props (the intial count) into the HTML
  50. .route(
  51. "/",
  52. get(move |State(ssr_state): State<SSRState>| async move { axum::body::Full::from(
  53. ssr_state.render(
  54. &ServeConfigBuilder::new(
  55. app,
  56. 0,
  57. )
  58. .build(),
  59. )
  60. )}),
  61. )
  62. // Render the application with a different intial count
  63. .route(
  64. "/:initial_count",
  65. get(move |Path(intial_count): Path<usize>, State(ssr_state): State<SSRState>| async move { axum::body::Full::from(
  66. ssr_state.render(
  67. &ServeConfigBuilder::new(
  68. app,
  69. intial_count,
  70. )
  71. .build(),
  72. )
  73. )}),
  74. )
  75. .with_state(SSRState::default())
  76. .into_make_service(),
  77. )
  78. .await
  79. .unwrap();
  80. });
  81. }
  82. }
  83. fn app(cx: Scope<usize>) -> Element {
  84. let mut count = use_state(cx, || *cx.props);
  85. cx.render(rsx! {
  86. h1 { "High-Five counter: {count}" }
  87. button { onclick: move |_| count += 1, "Up high!" }
  88. button { onclick: move |_| count -= 1, "Down low!" }
  89. button {
  90. onclick: move |_| {
  91. to_owned![count];
  92. let sc = cx.sc();
  93. async move {
  94. // Call the server function just like a local async function
  95. if let Ok(new_count) = double_server(sc, *count.current()).await {
  96. count.set(new_count);
  97. }
  98. }
  99. },
  100. "Double"
  101. }
  102. })
  103. }
  104. // We use the "getcbor" encoding to make caching easier
  105. #[server(DoubleServer, "", "getcbor")]
  106. async fn double_server(cx: DioxusServerContext, number: usize) -> Result<usize, ServerFnError> {
  107. // Perform some expensive computation or access a database on the server
  108. tokio::time::sleep(std::time::Duration::from_secs(1)).await;
  109. let result = number * 2;
  110. println!(
  111. "User Agent {:?}",
  112. cx.request_parts().headers.get("User-Agent")
  113. );
  114. // Set the cache control header to 1 hour on the post request
  115. cx.response_headers_mut()
  116. .insert("Cache-Control", "max-age=3600".parse().unwrap());
  117. // Get the server function state
  118. let server_fn_state = cx.get::<ServerFunctionState>().unwrap();
  119. let call_count = server_fn_state
  120. .call_count
  121. .fetch_add(1, std::sync::atomic::Ordering::SeqCst);
  122. println!("server functions have been called {call_count} times");
  123. println!("server calculated {result}");
  124. Ok(result)
  125. }