hello_world_ssr.rs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #![allow(unused)]
  2. #![allow(non_snake_case)]
  3. // ANCHOR: all
  4. // ANCHOR: main
  5. #![allow(non_snake_case)]
  6. use axum::{response::Html, routing::get, Router};
  7. // import the prelude to get access to the `rsx!` macro and the `Scope` and `Element` types
  8. use dioxus::prelude::*;
  9. #[tokio::main]
  10. async fn main() {
  11. let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 3000));
  12. println!("listening on http://{}", addr);
  13. axum::Server::bind(&addr)
  14. .serve(
  15. Router::new()
  16. .route("/", get(app_endpoint))
  17. .into_make_service(),
  18. )
  19. .await
  20. .unwrap();
  21. }
  22. // ANCHOR_END: main
  23. // ANCHOR: endpoint
  24. async fn app_endpoint() -> Html<String> {
  25. // render the rsx! macro to HTML
  26. Html(dioxus_ssr::render_lazy(rsx! {
  27. div { "hello world!" }
  28. }))
  29. }
  30. // ANCHOR_END: endpoint
  31. // ANCHOR: second_endpoint
  32. async fn second_app_endpoint() -> Html<String> {
  33. // create a component that renders a div with the text "hello world"
  34. fn app(cx: Scope) -> Element {
  35. cx.render(rsx!(div { "hello world" }))
  36. }
  37. // create a VirtualDom with the app component
  38. let mut app = VirtualDom::new(app);
  39. // rebuild the VirtualDom before rendering
  40. let _ = app.rebuild();
  41. // render the VirtualDom to HTML
  42. Html(dioxus_ssr::render(&app))
  43. }
  44. // ANCHOR_END: second_endpoint
  45. // ANCHOR: component
  46. // define a component that renders a div with the text "Hello, world!"
  47. fn App(cx: Scope) -> Element {
  48. cx.render(rsx! {
  49. div {
  50. "Hello, world!"
  51. }
  52. })
  53. }
  54. // ANCHOR_END: component
  55. // ANCHOR_END: all