main.rs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // This test is used by playwrite configured in the root of the repo
  2. // Tests:
  3. // - Server functions
  4. // - SSR
  5. // - Hydration
  6. #![allow(non_snake_case)]
  7. use dioxus::prelude::*;
  8. use dioxus_fullstack::prelude::*;
  9. use serde::{Deserialize, Serialize};
  10. fn main() {
  11. #[cfg(feature = "web")]
  12. dioxus_web::launch_with_props(
  13. app,
  14. get_root_props_from_document().unwrap_or_default(),
  15. dioxus_web::Config::new().hydrate(true),
  16. );
  17. #[cfg(feature = "ssr")]
  18. {
  19. // Start hot reloading
  20. hot_reload_init!(dioxus_hot_reload::Config::new().with_rebuild_callback(|| {
  21. execute::shell("dioxus build --features web")
  22. .spawn()
  23. .unwrap()
  24. .wait()
  25. .unwrap();
  26. execute::shell("cargo run --features ssr").spawn().unwrap();
  27. true
  28. }));
  29. tokio::runtime::Runtime::new()
  30. .unwrap()
  31. .block_on(async move {
  32. let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 3333));
  33. axum::Server::bind(&addr)
  34. .serve(
  35. axum::Router::new()
  36. .serve_dioxus_application(
  37. "",
  38. ServeConfigBuilder::new(app, AppProps { count: 12345 }).build(),
  39. )
  40. .into_make_service(),
  41. )
  42. .await
  43. .unwrap();
  44. });
  45. }
  46. }
  47. #[derive(Props, PartialEq, Debug, Default, Serialize, Deserialize, Clone)]
  48. struct AppProps {
  49. count: i32,
  50. }
  51. #[allow(unused)]
  52. fn app(cx: Scope<AppProps>) -> Element {
  53. let mut count = use_state(cx, || cx.props.count);
  54. let text = use_state(cx, || "...".to_string());
  55. cx.render(rsx! {
  56. h1 { "hello axum! {count}" }
  57. button {
  58. class: "increment-button",
  59. onclick: move |_| count += 1,
  60. "Increment"
  61. }
  62. button {
  63. class: "server-button",
  64. onclick: move |_| {
  65. to_owned![text];
  66. async move {
  67. if let Ok(data) = get_server_data().await {
  68. println!("Client received: {}", data);
  69. text.set(data.clone());
  70. post_server_data(data).await.unwrap();
  71. }
  72. }
  73. },
  74. "Run a server function!"
  75. }
  76. "Server said: {text}"
  77. })
  78. }
  79. #[server(PostServerData)]
  80. async fn post_server_data(data: String) -> Result<(), ServerFnError> {
  81. println!("Server received: {}", data);
  82. Ok(())
  83. }
  84. #[server(GetServerData)]
  85. async fn get_server_data() -> Result<String, ServerFnError> {
  86. Ok("Hello from the server!".to_string())
  87. }