tide.rs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //!
  2. //!
  3. //!
  4. use dioxus::virtual_dom::VirtualDom;
  5. use dioxus_core as dioxus;
  6. use dioxus_core::prelude::*;
  7. use dioxus_core_macro::*;
  8. use dioxus_hooks::use_state;
  9. use dioxus_html as dioxus_elements;
  10. use tide::{Request, Response};
  11. #[async_std::main]
  12. async fn main() -> Result<(), std::io::Error> {
  13. let mut app = tide::new();
  14. app.at("/:name").get(|req: Request<()>| async move {
  15. let initial_name: String = req
  16. .param("name")
  17. .map(|f| f.parse().unwrap_or("...?".to_string()))
  18. .unwrap_or("...?".to_string());
  19. let mut dom = VirtualDom::new_with_props(Example, ExampleProps { initial_name });
  20. dom.rebuild();
  21. Ok(Response::builder(200)
  22. .body(format!("{}", dioxus_ssr::render_vdom(&dom, |c| c)))
  23. .content_type(tide::http::mime::HTML)
  24. .build())
  25. });
  26. println!("Server available at [http://127.0.0.1:8080/bill]");
  27. app.listen("127.0.0.1:8080").await?;
  28. Ok(())
  29. }
  30. #[derive(PartialEq, Props)]
  31. struct ExampleProps {
  32. initial_name: String,
  33. }
  34. static Example: FC<ExampleProps> = |cx, props| {
  35. let dispaly_name = use_state(cx, move || props.initial_name.clone());
  36. cx.render(rsx! {
  37. div { class: "py-12 px-4 text-center w-full max-w-2xl mx-auto",
  38. span { class: "text-sm font-semibold"
  39. "Dioxus Example: Jack and Jill"
  40. }
  41. h2 { class: "text-5xl mt-2 mb-6 leading-tight font-semibold font-heading"
  42. "Hello, {dispaly_name}"
  43. }
  44. ul {
  45. {(0..10).map(|f| rsx!( li {"Element {f}"} ))}
  46. }
  47. }
  48. })
  49. };