main.rs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //! Run with:
  2. //!
  3. //! ```sh
  4. //! dx 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("dx 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. tokio::runtime::Runtime::new()
  35. .unwrap()
  36. .block_on(async move {
  37. let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 8080));
  38. axum::Server::bind(&addr)
  39. .serve(
  40. axum::Router::new()
  41. // Serve the dist/assets folder with the javascript and WASM files created by the CLI
  42. .serve_static_assets("./dist")
  43. // Register server functions
  44. .register_server_fns("")
  45. // Connect to the hot reload server
  46. .connect_hot_reload()
  47. // If the path is unknown, render the application
  48. .fallback(
  49. move |uri: http::uri::Uri, State(ssr_state): State<SSRState>| {
  50. let rendered = ssr_state.render(
  51. &ServeConfigBuilder::new(
  52. App,
  53. AppProps {
  54. route: Some(format!("http://{addr}{uri}")),
  55. },
  56. )
  57. .build(),
  58. );
  59. async move { axum::body::Full::from(rendered) }
  60. },
  61. )
  62. .with_state(SSRState::default())
  63. .into_make_service(),
  64. )
  65. .await
  66. .unwrap();
  67. });
  68. }
  69. }
  70. #[derive(Clone, Debug, Props, PartialEq, Serialize, Deserialize)]
  71. struct AppProps {
  72. route: Option<String>,
  73. }
  74. fn App(cx: Scope<AppProps>) -> Element {
  75. cx.render(rsx! {
  76. Router {
  77. initial_url: cx.props.route.clone(),
  78. Route { to: "/blog",
  79. Link {
  80. to: "/",
  81. "Go to counter"
  82. }
  83. table {
  84. tbody {
  85. for _ in 0..100 {
  86. tr {
  87. for _ in 0..100 {
  88. td { "hello world!" }
  89. }
  90. }
  91. }
  92. }
  93. }
  94. },
  95. // Fallback
  96. Route { to: "",
  97. Counter {}
  98. },
  99. }
  100. })
  101. }
  102. fn Counter(cx: Scope) -> Element {
  103. let mut count = use_state(cx, || 0);
  104. let text = use_state(cx, || "...".to_string());
  105. cx.render(rsx! {
  106. Link {
  107. to: "/blog",
  108. "Go to blog"
  109. }
  110. div{
  111. h1 { "High-Five counter: {count}" }
  112. button { onclick: move |_| count += 1, "Up high!" }
  113. button { onclick: move |_| count -= 1, "Down low!" }
  114. button {
  115. onclick: move |_| {
  116. to_owned![text];
  117. async move {
  118. if let Ok(data) = get_server_data().await {
  119. println!("Client received: {}", data);
  120. text.set(data.clone());
  121. post_server_data(data).await.unwrap();
  122. }
  123. }
  124. },
  125. "Run a server function"
  126. }
  127. "Server said: {text}"
  128. }
  129. })
  130. }
  131. #[server(PostServerData)]
  132. async fn post_server_data(data: String) -> Result<(), ServerFnError> {
  133. println!("Server received: {}", data);
  134. Ok(())
  135. }
  136. #[server(GetServerData)]
  137. async fn get_server_data() -> Result<String, ServerFnError> {
  138. Ok("Hello from the server!".to_string())
  139. }