warp.rs 1.0 KB

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