warp.rs 903 B

1234567891011121314151617181920212223242526272829303132
  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. pretty_env_logger::init();
  8. let addr = ([127, 0, 0, 1], 3030);
  9. // todo: compactify this routing under one liveview::app method
  10. let view = liveview::new(addr);
  11. let body = view.body();
  12. let routes = warp::path::end()
  13. .map(move || warp::reply::html(body.clone()))
  14. .or(warp::path("app")
  15. .and(warp::ws())
  16. .and(warp::any().map(move || view.clone()))
  17. .map(|ws: Ws, view: liveview::Liveview| {
  18. ws.on_upgrade(|socket| async move {
  19. view.upgrade(socket, app).await;
  20. })
  21. }));
  22. warp::serve(routes).run(addr).await;
  23. }
  24. fn app(cx: Scope) -> Element {
  25. cx.render(LazyNodes::new(|f| f.text(format_args!("hello world!"))))
  26. }