tide.rs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 mut dom = VirtualDom::new_with_props(Example, ExampleProps { initial_name });
  19. dom.rebuild();
  20. Ok(Response::builder(200)
  21. .body(format!("{}", dioxus_ssr::render_vdom(&dom, |c| c)))
  22. .content_type(tide::http::mime::HTML)
  23. .build())
  24. });
  25. println!("Server available at [http://127.0.0.1:8080/bill]");
  26. app.listen("127.0.0.1:8080").await?;
  27. Ok(())
  28. }
  29. #[derive(PartialEq, Props)]
  30. struct ExampleProps {
  31. initial_name: String,
  32. }
  33. static Example: FC<ExampleProps> = |cx, props| {
  34. let dispaly_name = use_state(cx, move || props.initial_name.clone());
  35. cx.render(rsx! {
  36. div { class: "py-12 px-4 text-center w-full max-w-2xl mx-auto",
  37. span { class: "text-sm font-semibold"
  38. "Dioxus Example: Jack and Jill"
  39. }
  40. h2 { class: "text-5xl mt-2 mb-6 leading-tight font-semibold font-heading"
  41. "Hello, {dispaly_name}"
  42. }
  43. ul {
  44. {(0..10).map(|f| rsx!( li {"Element {f}"} ))}
  45. }
  46. }
  47. })
  48. };