chore: Add htmx on an index.html root page

This commit is contained in:
Florian Briand 2024-07-03 17:50:29 +02:00
parent 12973bc840
commit 1f1eea31a0
Signed by: florian_briand
GPG Key ID: CC981B9E6B98E70B
2 changed files with 30 additions and 9 deletions

View File

@ -4,21 +4,30 @@ use axum::{
}; };
use askama_axum::Template; use askama_axum::Template;
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/", get(index))
.route("/hello", get(hello_world));
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
}
#[derive(Template)]
#[template(path = "index.html")]
struct IndexTemplate {}
async fn index() -> IndexTemplate {
IndexTemplate {}
}
#[derive(Template)] #[derive(Template)]
#[template(path = "hello.html")] #[template(path = "hello.html")]
struct HelloTemplate<'a> { struct HelloTemplate<'a> {
name: &'a str, name: &'a str,
} }
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/", get(hello_world));
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
}
async fn hello_world() -> HelloTemplate<'static> { async fn hello_world() -> HelloTemplate<'static> {
HelloTemplate { name: "world" } HelloTemplate { name: "world" }
} }

12
templates/index.html Normal file
View File

@ -0,0 +1,12 @@
<html>
<head>
<title>Krys4lide</title>
<script src="https://unpkg.com/htmx.org@2.0.0/dist/htmx.js" integrity="sha384-Xh+GLLi0SMFPwtHQjT72aPG19QvKB8grnyRbYBNIdHWc2NkCrz65jlU7YrzO6qRp" crossorigin="anonymous"></script>
</head>
<body>
<h1>Krys4lide</h1>
<button hx-get="/hello" hx-target="#hello">Hello!</button>
<div id="hello"></div>
</body>
</html>