axum.rs 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. #[cfg(not(feature = "axum"))]
  2. fn main() {}
  3. #[cfg(feature = "axum")]
  4. #[tokio::main]
  5. async fn main() {
  6. use axum::{extract::ws::WebSocketUpgrade, response::Html, routing::get, Router};
  7. use dioxus_core::{Element, LazyNodes, Scope};
  8. pretty_env_logger::init();
  9. fn app(cx: Scope) -> Element {
  10. cx.render(LazyNodes::new(|f| f.text(format_args!("hello world!"))))
  11. }
  12. let addr: std::net::SocketAddr = ([127, 0, 0, 1], 3030).into();
  13. let view = dioxus_liveview::new(addr);
  14. let body = view.body("<title>Dioxus Liveview</title>");
  15. let app = Router::new()
  16. .route("/", get(move || async { Html(body) }))
  17. .route(
  18. "/app",
  19. get(move |ws: WebSocketUpgrade| async move {
  20. ws.on_upgrade(move |socket| async move {
  21. view.upgrade_axum(socket, app).await;
  22. })
  23. }),
  24. );
  25. axum::Server::bind(&addr.to_string().parse().unwrap())
  26. .serve(app.into_make_service())
  27. .await
  28. .unwrap();
  29. }