2022-12-19 21:11:49 +00:00
|
|
|
use axum::{extract::ws::WebSocketUpgrade, response::Html, routing::get, Router};
|
|
|
|
use dioxus::prelude::*;
|
|
|
|
|
|
|
|
fn app(cx: Scope) -> Element {
|
|
|
|
let mut num = use_state(cx, || 0);
|
|
|
|
|
|
|
|
cx.render(rsx! {
|
|
|
|
div {
|
|
|
|
"hello axum! {num}"
|
|
|
|
button { onclick: move |_| num += 1, "Increment" }
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-04-24 02:13:43 +00:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2022-06-20 06:24:39 +00:00
|
|
|
pretty_env_logger::init();
|
2022-04-24 02:13:43 +00:00
|
|
|
|
2022-06-20 06:24:39 +00:00
|
|
|
let addr: std::net::SocketAddr = ([127, 0, 0, 1], 3030).into();
|
2022-04-24 02:13:43 +00:00
|
|
|
|
2022-12-19 21:11:49 +00:00
|
|
|
let view = dioxus_liveview::LiveViewPool::new();
|
2022-04-24 02:13:43 +00:00
|
|
|
|
2022-06-20 06:24:39 +00:00
|
|
|
let app = Router::new()
|
|
|
|
.route(
|
2022-12-19 21:11:49 +00:00
|
|
|
"/",
|
|
|
|
get(move || async move {
|
|
|
|
Html(format!(
|
|
|
|
r#"
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
2023-03-14 21:21:39 +00:00
|
|
|
<head> <title>Dioxus LiveView with axum</title> </head>
|
2022-12-19 21:11:49 +00:00
|
|
|
<body> <div id="main"></div> </body>
|
|
|
|
{glue}
|
|
|
|
</html>
|
|
|
|
"#,
|
|
|
|
glue = dioxus_liveview::interpreter_glue(&format!("ws://{addr}/ws"))
|
|
|
|
))
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/ws",
|
2022-06-20 06:24:39 +00:00
|
|
|
get(move |ws: WebSocketUpgrade| async move {
|
|
|
|
ws.on_upgrade(move |socket| async move {
|
2022-12-19 21:11:49 +00:00
|
|
|
_ = view.launch(dioxus_liveview::axum_socket(socket), app).await;
|
2022-06-20 06:24:39 +00:00
|
|
|
})
|
|
|
|
}),
|
|
|
|
);
|
2022-12-19 21:11:49 +00:00
|
|
|
|
2023-01-28 02:35:46 +00:00
|
|
|
println!("Listening on http://{addr}");
|
2022-12-19 21:11:49 +00:00
|
|
|
|
2022-06-20 06:24:39 +00:00
|
|
|
axum::Server::bind(&addr.to_string().parse().unwrap())
|
|
|
|
.serve(app.into_make_service())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-04-24 02:13:43 +00:00
|
|
|
}
|