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
/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"
[dependencies]
askama = { version = "0.12", features = ["with-axum"] }
askama_axum = "0.3"
axum = "0.6"
dioxus = "0.4"
dioxus-liveview = { version = "0.4", features = ["axum"] }
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 dioxus::prelude::*;
use std::net::SocketAddr;
fn app(cx: Scope) -> Element {
cx.render(rsx! {
div {
"Hello, world!"
}
})
use askama_axum::Template;
use axum::{routing::get, Router};
#[derive(Template)]
#[template(path = "hello.html")]
struct HelloTemplate {}
async fn hello() -> HelloTemplate {
HelloTemplate {}
}
#[tokio::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()
// The root route contains the glue code to connect to the WebSocket
.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;
})
}),
);
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
tracing::info!("listening on {}", addr);
println!("Listening on http://{addr}");
axum::Server::bind(&addr.to_string().parse().unwrap())
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.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 %}