warp.rs 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. use dioxus_core::{Element, LazyNodes, Scope};
  2. use dioxus_liveview as liveview;
  3. use warp::ws::Ws;
  4. use warp::Filter;
  5. #[tokio::main]
  6. async fn main() {
  7. #[cfg(feature = "warp")]
  8. {
  9. pretty_env_logger::init();
  10. let addr = ([127, 0, 0, 1], 3030);
  11. // todo: compactify this routing under one liveview::app method
  12. let view = liveview::new(addr);
  13. let body = view.body("<title>Dioxus LiveView</title>");
  14. let routes = warp::path::end()
  15. .map(move || warp::reply::html(body.clone()))
  16. .or(warp::path("app")
  17. .and(warp::ws())
  18. .and(warp::any().map(move || view.clone()))
  19. .map(|ws: Ws, view: liveview::Liveview| {
  20. ws.on_upgrade(|socket| async move {
  21. view.upgrade(socket, app).await;
  22. })
  23. }));
  24. warp::serve(routes).run(addr).await;
  25. }
  26. }
  27. fn app(cx: Scope) -> Element {
  28. cx.render(LazyNodes::new(|f| f.text(format_args!("hello world!"))))
  29. }