main.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //! Run with:
  2. //!
  3. //! ```sh
  4. //! dioxus build --features web
  5. //! cargo run --features ssr --no-default-features
  6. //! ```
  7. #![allow(non_snake_case)]
  8. use dioxus::prelude::*;
  9. use dioxus_fullstack::prelude::*;
  10. use dioxus_router::*;
  11. use serde::{Deserialize, Serialize};
  12. fn main() {
  13. #[cfg(feature = "web")]
  14. dioxus_web::launch_with_props(
  15. App,
  16. AppProps { route: None },
  17. dioxus_web::Config::new().hydrate(true),
  18. );
  19. #[cfg(feature = "ssr")]
  20. {
  21. // Start hot reloading
  22. hot_reload_init!(dioxus_hot_reload::Config::new().with_rebuild_callback(|| {
  23. execute::shell("dioxus build --features web")
  24. .spawn()
  25. .unwrap()
  26. .wait()
  27. .unwrap();
  28. execute::shell("cargo run --features ssr --no-default-features")
  29. .spawn()
  30. .unwrap();
  31. true
  32. }));
  33. use axum::extract::State;
  34. PostServerData::register().unwrap();
  35. GetServerData::register().unwrap();
  36. tokio::runtime::Runtime::new()
  37. .unwrap()
  38. .block_on(async move {
  39. let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 8080));
  40. axum::Server::bind(&addr)
  41. .serve(
  42. axum::Router::new()
  43. // Serve the dist/assets folder with the javascript and WASM files created by the CLI
  44. .serve_static_assets("./dist")
  45. // Register server functions
  46. .register_server_fns("")
  47. // Connect to the hot reload server
  48. .connect_hot_reload()
  49. // If the path is unknown, render the application
  50. .fallback(
  51. move |uri: http::uri::Uri, State(ssr_state): State<SSRState>| {
  52. let rendered = ssr_state.render(
  53. &ServeConfigBuilder::new(
  54. App,
  55. AppProps {
  56. route: Some(format!("http://{addr}{uri}")),
  57. },
  58. )
  59. .build(),
  60. );
  61. async move { axum::body::Full::from(rendered) }
  62. },
  63. )
  64. .with_state(SSRState::default())
  65. .into_make_service(),
  66. )
  67. .await
  68. .unwrap();
  69. });
  70. }
  71. }
  72. #[derive(Clone, Debug, Props, PartialEq, Serialize, Deserialize)]
  73. struct AppProps {
  74. route: Option<String>,
  75. }
  76. fn App(cx: Scope<AppProps>) -> Element {
  77. cx.render(rsx! {
  78. Router {
  79. initial_url: cx.props.route.clone(),
  80. Route { to: "/blog",
  81. Link {
  82. to: "/",
  83. "Go to counter"
  84. }
  85. table {
  86. tbody {
  87. for _ in 0..100 {
  88. tr {
  89. for _ in 0..100 {
  90. td { "hello world!" }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. },
  97. // Fallback
  98. Route { to: "",
  99. Counter {}
  100. },
  101. }
  102. })
  103. }
  104. fn Counter(cx: Scope) -> Element {
  105. let mut count = use_state(cx, || 0);
  106. let text = use_state(cx, || "...".to_string());
  107. cx.render(rsx! {
  108. Link {
  109. to: "/blog",
  110. "Go to blog"
  111. }
  112. div{
  113. h1 { "High-Five counter: {count}" }
  114. button { onclick: move |_| count += 1, "Up high!" }
  115. button { onclick: move |_| count -= 1, "Down low!" }
  116. button {
  117. onclick: move |_| {
  118. to_owned![text];
  119. async move {
  120. if let Ok(data) = get_server_data().await {
  121. println!("Client received: {}", data);
  122. text.set(data.clone());
  123. post_server_data(data).await.unwrap();
  124. }
  125. }
  126. },
  127. "Run a server function"
  128. }
  129. "Server said: {text}"
  130. }
  131. })
  132. }
  133. #[server(PostServerData)]
  134. async fn post_server_data(data: String) -> Result<(), ServerFnError> {
  135. println!("Server received: {}", data);
  136. Ok(())
  137. }
  138. #[server(GetServerData)]
  139. async fn get_server_data() -> Result<String, ServerFnError> {
  140. Ok("Hello from the server!".to_string())
  141. }