tide.rs 1.5 KB

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