refacto: extract livereload layer setup into a function
Co-authored-by: kosssi <github@fafaru.com>
This commit is contained in:
parent
dcb4a7680e
commit
60a409f20a
@ -1,7 +1,9 @@
|
|||||||
use ::app::get_router;
|
use ::app::get_router;
|
||||||
|
use axum::body::Body;
|
||||||
use axum::http::Request;
|
use axum::http::Request;
|
||||||
use listenfd::ListenFd;
|
use listenfd::ListenFd;
|
||||||
use notify::Watcher;
|
use notify::Watcher;
|
||||||
|
use tower_livereload::predicate::Predicate;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
@ -9,8 +11,12 @@ use tower_livereload::LiveReloadLayer;
|
|||||||
|
|
||||||
/// Nous filtrons les requêtes de `htmx` pour ne pas inclure le script _JS_ qui gère le rechargement
|
/// Nous filtrons les requêtes de `htmx` pour ne pas inclure le script _JS_ qui gère le rechargement
|
||||||
/// Voir https://github.com/leotaku/tower-livereload/pull/3
|
/// Voir https://github.com/leotaku/tower-livereload/pull/3
|
||||||
fn not_htmx_predicate<T>(req: &Request<T>) -> bool {
|
#[derive(Copy, Clone)]
|
||||||
!req.headers().contains_key("hx-request")
|
struct NotHtmxPredicate;
|
||||||
|
impl<T> Predicate<Request<T>> for NotHtmxPredicate {
|
||||||
|
fn check(&mut self, req: &Request<T>) -> bool {
|
||||||
|
!(req.headers().contains_key("hx-request"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const DEFAULT_LISTENER: &str = "localhost:3000";
|
const DEFAULT_LISTENER: &str = "localhost:3000";
|
||||||
@ -28,22 +34,24 @@ async fn get_tcp_listener() -> TcpListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_livereload_layer(templates_path: &Path) -> LiveReloadLayer<NotHtmxPredicate> {
|
||||||
|
let livereload = LiveReloadLayer::new();
|
||||||
|
let reloader = livereload.reloader();
|
||||||
|
let mut watcher = notify::recommended_watcher(move |_| reloader.reload()).unwrap();
|
||||||
|
watcher
|
||||||
|
.watch(templates_path, notify::RecursiveMode::Recursive)
|
||||||
|
.unwrap();
|
||||||
|
livereload.request_predicate::<Body, NotHtmxPredicate>(NotHtmxPredicate)
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
|
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
|
||||||
let assets_path = Path::new(&manifest_dir).join("assets");
|
let assets_path = Path::new(&manifest_dir).join("assets");
|
||||||
let templates_path = Path::new(&manifest_dir).join("templates");
|
let templates_path = Path::new(&manifest_dir).join("templates");
|
||||||
|
|
||||||
// Setup the livereload
|
let livereload_layer = get_livereload_layer(&templates_path);
|
||||||
let livereload = LiveReloadLayer::new();
|
let router = get_router(assets_path.as_path()).layer(livereload_layer);
|
||||||
let reloader = livereload.reloader();
|
|
||||||
let mut watcher = notify::recommended_watcher(move |_| reloader.reload()).unwrap();
|
|
||||||
watcher
|
|
||||||
.watch(&templates_path, notify::RecursiveMode::Recursive)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let router =
|
|
||||||
get_router(assets_path.as_path()).layer(livereload.request_predicate(not_htmx_predicate));
|
|
||||||
|
|
||||||
let listener: TcpListener = get_tcp_listener().await;
|
let listener: TcpListener = get_tcp_listener().await;
|
||||||
println!("Listening on: http://{}", listener.local_addr().unwrap());
|
println!("Listening on: http://{}", listener.local_addr().unwrap());
|
||||||
|
Loading…
Reference in New Issue
Block a user