Files
dioxus/packages/playwright-tests/liveview/src/main.rs
T
Jonathan Kelley 9243ef4128 redirects, middleware, Transport, set/capture error status (#4751)
* wip: redirect

* redirect example

* fix file upload not in a form

* wip

* deny missing cfgs

* wip: add router fallback but it's not working

* fix suspense bug after rebuilds that queued work

* more cleanup

* add test for error codes

* add helpful comment

* bring back middleware annotation

* fix cfg

* add server feature

* fix doctest

* just disable liveview test for now

* fix liveview

* dont send init

* cache more playwright!

* add more pkgs to cache
2025-10-10 21:43:57 -07:00

82 lines
2.4 KiB
Rust

// This test is used by playwright configured in the root of the repo
use axum::{extract::ws::WebSocketUpgrade, response::Html, routing::get, Router};
use dioxus::{logger::tracing::Level, prelude::*};
fn app() -> Element {
let mut num = use_signal(|| 0);
rsx! {
div {
"hello axum! {num}"
button { onclick: move |_| num += 1, "Increment" }
}
svg { circle { cx: 50, cy: 50, r: 40, stroke: "green", fill: "yellow" } }
div { class: "raw-attribute-div", "raw-attribute": "raw-attribute-value" }
div { class: "hidden-attribute-div", hidden: true }
div {
class: "dangerous-inner-html-div",
dangerous_inner_html: "<p>hello dangerous inner html</p>"
}
input { value: "hello input" }
div { class: "style-div", color: "red", "colored text" }
OnMounted {}
}
}
#[component]
fn OnMounted() -> Element {
let mut mounted_triggered_count = use_signal(|| 0);
rsx! {
div {
class: "onmounted-div",
onmounted: move |_| {
mounted_triggered_count += 1;
},
"onmounted was called {mounted_triggered_count} times"
}
}
}
#[tokio::main]
async fn main() {
_ = dioxus::logger::init(Level::DEBUG);
let addr: std::net::SocketAddr = ([127, 0, 0, 1], 3030).into();
let view = dioxus_liveview::LiveViewPool::new();
let app = Router::new()
.route(
"/",
get(move || async move {
Html(format!(
r#"
<!DOCTYPE html>
<html>
<head> <title>Dioxus LiveView with axum</title> </head>
<body> <div id="main"></div> </body>
{glue}
</html>
"#,
glue = dioxus_liveview::interpreter_glue(&format!("ws://{addr}/ws"))
))
}),
)
.route(
"/ws",
get(move |ws: WebSocketUpgrade| async move {
ws.on_upgrade(move |socket| async move {
_ = view.launch(dioxus_liveview::axum_socket(socket), app).await;
})
}),
);
println!("Listening on http://{addr}");
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
axum::serve(listener, app.into_make_service())
.await
.unwrap();
}