tide.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //!
  2. //!
  3. //!
  4. use std::{borrow::Borrow, rc::Rc, sync::Arc};
  5. use async_std::{prelude::*, sync::RwLock};
  6. use dioxus::{events::on::MouseEvent, virtual_dom::VirtualDom};
  7. use dioxus_core::prelude::*;
  8. use tide::{Body, Request, Response};
  9. use tide_websockets::{Message, WebSocket};
  10. #[derive(PartialEq, Props)]
  11. struct ExampleProps {
  12. initial_name: String,
  13. }
  14. static Example: FC<ExampleProps> = |ctx, props| {
  15. let dispaly_name = use_state_new(&ctx, move || props.initial_name);
  16. let buttons = ["Jack", "Jill", "Bob"].iter().map(|name| {
  17. rsx!{
  18. button {
  19. class: "inline-block py-4 px-8 mr-6 leading-none text-white bg-indigo-600 hover:bg-indigo-900 font-semibold rounded shadow"
  20. onmouseover: move |_| dispaly_name.set(name.to_string())
  21. "{name}"
  22. }
  23. }
  24. });
  25. ctx.render(rsx! {
  26. div {
  27. class: "py-12 px-4 text-center w-full max-w-2xl mx-auto"
  28. span {
  29. class: "text-sm font-semibold"
  30. "Dioxus Example: Jack and Jill"
  31. }
  32. h2 {
  33. class: "text-5xl mt-2 mb-6 leading-tight font-semibold font-heading"
  34. "Hello, {dispaly_name}"
  35. }
  36. }
  37. })
  38. };
  39. const TEMPLATE: &str = include_str!("./template.html");
  40. // static VDOM: Arc<RwLock<VirtualDom>> = Arc::new(RwLock::new(VirtualDom::new_with_props(
  41. // Example,
  42. // ExampleProps {
  43. // initial_name: "asd".to_string(),
  44. // },
  45. // )));
  46. #[async_std::main]
  47. async fn main() -> Result<(), std::io::Error> {
  48. let mut app = tide::new();
  49. let (se, re) = async_std::channel::unbounded::<()>();
  50. async_std::task::spawn(async move {
  51. let dom = VirtualDom::new_with_props(
  52. Example,
  53. ExampleProps {
  54. initial_name: "asd".to_string(),
  55. },
  56. );
  57. while let Ok(msg) = re.recv().await {
  58. //
  59. }
  60. });
  61. // app.at("/").get(|_| async { Ok(Body::from_file())});
  62. app.at("/").get(|_| async {
  63. //
  64. let response = Response::builder(200)
  65. .body(TEMPLATE)
  66. .content_type(tide::http::mime::HTML)
  67. .build();
  68. Ok(response)
  69. });
  70. app.at("/session/:name")
  71. .get(WebSocket::new(|req: Request<()>, mut stream| async move {
  72. let initial_name: String = req.param("name")?.parse().unwrap_or("...?".to_string());
  73. // {
  74. // let a = Rc::new(());
  75. // }
  76. // let dom = VirtualDom::new_with_props(Example, ExampleProps { initial_name });
  77. // let g = RwLock::new(Rc::new(10));
  78. // drop(g);
  79. while let Some(Ok(Message::Text(input))) = stream.next().await {
  80. let output: String = input.chars().rev().collect();
  81. stream
  82. .send_string(format!("{} | {}", &input, &output))
  83. .await?;
  84. }
  85. Ok(())
  86. }));
  87. app.listen("127.0.0.1:8080").await?;
  88. Ok(())
  89. }