Ajoute un livereload de développement
Feature cargo `livereload` (tower-livereload, désactivée par défaut) : script injecté dans les pages hors requêtes htmx, assets en no-cache. À lancer avec watchexec (cf. README). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fdba078b51
commit
f4c94f9b5b
5 files changed
+47
-2
No files matched your search
Generated
+15
@@ -1697,6 +1697,7 @@ dependencies = [
|
|||||||
"tokio",
|
"tokio",
|
||||||
"tower",
|
"tower",
|
||||||
"tower-http",
|
"tower-http",
|
||||||
|
"tower-livereload",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
]
|
]
|
||||||
@@ -1824,6 +1825,20 @@ version = "0.3.3"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e"
|
checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tower-livereload"
|
||||||
|
version = "0.10.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7df210bd982165dee3c20e31deed79a3585ee6b255c070d9c73ae015cc40d2f3"
|
||||||
|
dependencies = [
|
||||||
|
"bytes",
|
||||||
|
"http",
|
||||||
|
"http-body",
|
||||||
|
"pin-project-lite",
|
||||||
|
"tokio",
|
||||||
|
"tower",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tower-service"
|
name = "tower-service"
|
||||||
version = "0.3.3"
|
version = "0.3.3"
|
||||||
|
|||||||
@@ -15,9 +15,14 @@ sqlx = { version = "0.8", default-features = false, features = ["runtime-tokio",
|
|||||||
thiserror = "2.0.21"
|
thiserror = "2.0.21"
|
||||||
tokio = { version = "1.54.1", features = ["full"] }
|
tokio = { version = "1.54.1", features = ["full"] }
|
||||||
tower-http = { version = "0.7.1", features = ["trace"] }
|
tower-http = { version = "0.7.1", features = ["trace"] }
|
||||||
|
tower-livereload = { version = "0.10.3", optional = true }
|
||||||
tracing = "0.1.44"
|
tracing = "0.1.44"
|
||||||
tracing-subscriber = { version = "0.3.23", features = ["env-filter"] }
|
tracing-subscriber = { version = "0.3.23", features = ["env-filter"] }
|
||||||
|
|
||||||
|
[features]
|
||||||
|
# Recharge le navigateur au redémarrage du serveur (dev uniquement).
|
||||||
|
livereload = ["dep:tower-livereload"]
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
http-body-util = "0.1.5"
|
http-body-util = "0.1.5"
|
||||||
tower = { version = "0.5.3", features = ["util"] }
|
tower = { version = "0.5.3", features = ["util"] }
|
||||||
@@ -53,6 +53,16 @@ curl -XPOST localhost:7878/api/timer/start -H 'content-type: application/json' -
|
|||||||
|
|
||||||
## Développement
|
## Développement
|
||||||
|
|
||||||
|
Rechargement automatique (recompile, redémarre et rafraîchit le navigateur) :
|
||||||
|
|
||||||
|
```sh
|
||||||
|
cargo install watchexec-cli # une fois
|
||||||
|
watchexec -r -e rs,html,css,js,sql -- cargo run --features livereload
|
||||||
|
```
|
||||||
|
|
||||||
|
La feature `livereload` injecte un script dans les pages (pas dans les fragments
|
||||||
|
htmx) et désactive le cache des assets ; elle n'est jamais active par défaut.
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cargo test
|
cargo test
|
||||||
cargo clippy --all-targets -- -D warnings
|
cargo clippy --all-targets -- -D warnings
|
||||||
|
|||||||
+9
-1
@@ -20,7 +20,15 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
.await
|
.await
|
||||||
.with_context(|| format!("cannot listen on {}", config.addr))?;
|
.with_context(|| format!("cannot listen on {}", config.addr))?;
|
||||||
tracing::info!(db = %config.database_url, "Tempus disponible sur http://{}", config.addr);
|
tracing::info!(db = %config.database_url, "Tempus disponible sur http://{}", config.addr);
|
||||||
axum::serve(listener, routes::app(pool))
|
let app = routes::app(pool);
|
||||||
|
#[cfg(feature = "livereload")]
|
||||||
|
let app = app.layer(
|
||||||
|
// Les fragments htmx ne doivent pas recevoir le script injecté.
|
||||||
|
tower_livereload::LiveReloadLayer::new().request_predicate(
|
||||||
|
|req: &axum::http::Request<axum::body::Body>| !req.headers().contains_key("hx-request"),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
axum::serve(listener, app)
|
||||||
.with_graceful_shutdown(async {
|
.with_graceful_shutdown(async {
|
||||||
let _ = tokio::signal::ctrl_c().await;
|
let _ = tokio::signal::ctrl_c().await;
|
||||||
})
|
})
|
||||||
|
|||||||
+8
-1
@@ -56,11 +56,18 @@ pub fn app(pool: SqlitePool) -> Router {
|
|||||||
.with_state(pool)
|
.with_state(pool)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// En livereload, le CSS/JS doit être relu à chaque rechargement.
|
||||||
|
const CACHE_CONTROL: &str = if cfg!(feature = "livereload") {
|
||||||
|
"no-cache"
|
||||||
|
} else {
|
||||||
|
"public, max-age=3600"
|
||||||
|
};
|
||||||
|
|
||||||
async fn asset(content_type: &'static str, body: &'static str) -> impl IntoResponse {
|
async fn asset(content_type: &'static str, body: &'static str) -> impl IntoResponse {
|
||||||
(
|
(
|
||||||
[
|
[
|
||||||
(header::CONTENT_TYPE, content_type),
|
(header::CONTENT_TYPE, content_type),
|
||||||
(header::CACHE_CONTROL, "public, max-age=3600"),
|
(header::CACHE_CONTROL, CACHE_CONTROL),
|
||||||
],
|
],
|
||||||
body,
|
body,
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in new issue
Block a user