This commit is contained in:
Hamcha 2023-11-18 14:29:19 +01:00
parent 633c74874d
commit c41fe3e7dd
Signed by: hamcha
GPG key ID: 1669C533B8CF6D89
6 changed files with 189 additions and 899 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
/target /target
/dist

1010
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -4,7 +4,9 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
askama = { version = "0.12", features = ["with-axum"] }
askama_axum = "0.3"
axum = "0.6" axum = "0.6"
dioxus = "0.4"
dioxus-liveview = { version = "0.4", features = ["axum"] }
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["full"] }
tracing = "0.1"
tracing-subscriber = "0.3"

View file

@ -1,53 +1,26 @@
use axum::{extract::WebSocketUpgrade, response::Html, routing::get, Router}; use std::net::SocketAddr;
use dioxus::prelude::*;
fn app(cx: Scope) -> Element { use askama_axum::Template;
cx.render(rsx! { use axum::{routing::get, Router};
div {
"Hello, world!" #[derive(Template)]
} #[template(path = "hello.html")]
}) struct HelloTemplate {}
async fn hello() -> HelloTemplate {
HelloTemplate {}
} }
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let addr: std::net::SocketAddr = ([127, 0, 0, 1], 3030).into(); tracing_subscriber::fmt::init();
let view = dioxus_liveview::LiveViewPool::new(); let app = Router::new().route("/", get(hello));
let app = Router::new() let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
// The root route contains the glue code to connect to the WebSocket tracing::info!("listening on {}", addr);
.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>
"#,
// Create the glue code to connect to the WebSocket on the "/ws" route
glue = dioxus_liveview::interpreter_glue(&format!("ws://{addr}/ws"))
))
}),
)
// The WebSocket route is what Dioxus uses to communicate with the browser
.route(
"/ws",
get(move |ws: WebSocketUpgrade| async move {
ws.on_upgrade(move |socket| async move {
// When the WebSocket is upgraded, launch the LiveView with the app component
_ = view.launch(dioxus_liveview::axum_socket(socket), app).await;
})
}),
);
println!("Listening on http://{addr}"); axum::Server::bind(&addr)
axum::Server::bind(&addr.to_string().parse().unwrap())
.serve(app.into_make_service()) .serve(app.into_make_service())
.await .await
.unwrap(); .unwrap();

11
templates/base.html Normal file
View file

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>staxman</title>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>

3
templates/hello.html Normal file
View file

@ -0,0 +1,3 @@
{% extends "base.html" %} {% block content %}
<main></main>
{% endblock %}