feat: embed htmx
This commit is contained in:
parent
35f1433e40
commit
412f4cd2c7
6 changed files with 128 additions and 10 deletions
|
|
@ -4,6 +4,7 @@ use std::sync::Arc;
|
|||
|
||||
use r2d2_sqlite::{rusqlite, SqliteConnectionManager};
|
||||
use tera::Tera;
|
||||
use tower_http::compression::CompressionLayer;
|
||||
|
||||
pub type DbPool = r2d2::Pool<SqliteConnectionManager>;
|
||||
|
||||
|
|
@ -11,6 +12,10 @@ const MIGRATIONS: [&str; 2] = [
|
|||
include_str!("migrations/000_initial.sql"),
|
||||
include_str!("migrations/001_plants.sql"),
|
||||
];
|
||||
const STATIC_FILES: [(&str, &'static str); 1] = [(
|
||||
"htmx_2.0.4.min.js",
|
||||
include_str!("static/htmx_2.0.4.min.js"),
|
||||
)];
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Context {
|
||||
|
|
@ -31,7 +36,7 @@ async fn main() {
|
|||
pool,
|
||||
tera: Arc::new(tera),
|
||||
};
|
||||
let app = server::app(ctx);
|
||||
let app = server::app(ctx).layer(CompressionLayer::new().br(true).gzip(true));
|
||||
|
||||
let address = "0.0.0.0:8000";
|
||||
|
||||
|
|
@ -80,6 +85,7 @@ fn load_templates() -> Tera {
|
|||
|
||||
tera.add_raw_templates(vec![
|
||||
("index.html", include_str!("templates/index.html")),
|
||||
("plants_ul.html", include_str!("templates/plants_ul.html")),
|
||||
("plant_li.html", include_str!("templates/plant_li.html")),
|
||||
])
|
||||
.unwrap();
|
||||
|
|
|
|||
|
|
@ -30,10 +30,15 @@ impl TryFrom<&Row<'_>> for Plant {
|
|||
}
|
||||
|
||||
pub fn app(ctx: crate::Context) -> axum::Router {
|
||||
Router::new()
|
||||
let mut router = Router::new()
|
||||
.route("/", get(get_index))
|
||||
.route("/plants", post(post_plants))
|
||||
.with_state(ctx)
|
||||
.route("/plants", post(post_plants));
|
||||
|
||||
for (name, content) in crate::STATIC_FILES {
|
||||
router = router.route(&format!("/static/{}", name), get(content))
|
||||
}
|
||||
|
||||
router.with_state(ctx)
|
||||
}
|
||||
|
||||
async fn get_index(State(ctx): State<crate::Context>) -> Html<String> {
|
||||
|
|
|
|||
1
src/static/htmx_2.0.4.min.js
vendored
Normal file
1
src/static/htmx_2.0.4.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
|
|
@ -1,15 +1,12 @@
|
|||
<html>
|
||||
<head>
|
||||
<script src="https://unpkg.com/htmx.org@2.0.4" integrity="sha384-HGfztofotfshcF7+8n44JQL2oJmowVChPTg48S+jvZoztPfvwD79OC/LTtG6dMp+" crossorigin="anonymous"></script>
|
||||
<script src="/static/htmx_2.0.4.min.js" integrity="sha384-HGfztofotfshcF7+8n44JQL2oJmowVChPTg48S+jvZoztPfvwD79OC/LTtG6dMp+"></script>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Calathea</h1>
|
||||
<h2>Plants</h2>
|
||||
<ul id="plants">
|
||||
{% for plant in plants %}
|
||||
<li>{{ plant.name }} ({{ plant.species }})</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% include "plants_ul.html" %}
|
||||
<h3>Add new plant</h3>
|
||||
<form hx-post="/plants" hx-target="#plants" hx-swap="beforeend">
|
||||
<label for="name">Name:</label>
|
||||
<input type="text" id="name" name="name"></br>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue