2024-07-24 21:59:24 +02:00
|
|
|
use ::app::get_router;
|
2024-08-09 12:55:20 +02:00
|
|
|
use axum::body::Body;
|
2024-08-08 22:19:24 +02:00
|
|
|
use axum::http::Request;
|
2024-08-08 22:14:11 +02:00
|
|
|
use listenfd::ListenFd;
|
2024-08-08 22:19:24 +02:00
|
|
|
use notify::Watcher;
|
2024-07-23 20:08:45 +02:00
|
|
|
use std::path::Path;
|
2024-08-14 10:40:41 +02:00
|
|
|
use std::{env, io};
|
|
|
|
use thiserror::Error;
|
2024-08-08 22:14:11 +02:00
|
|
|
use tokio::net::TcpListener;
|
2024-08-09 12:55:20 +02:00
|
|
|
use tower_livereload::predicate::Predicate;
|
2024-08-08 22:19:24 +02:00
|
|
|
use tower_livereload::LiveReloadLayer;
|
|
|
|
|
2024-08-14 10:40:41 +02:00
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum AppError {
|
|
|
|
#[error("Unable to bind to TCP listener")]
|
|
|
|
TCPListener(#[from] std::io::Error),
|
|
|
|
#[error("Error with the notify watcher")]
|
|
|
|
NotifyWatcher(#[from] notify::Error),
|
|
|
|
#[error("Missing environment variable {var}")]
|
|
|
|
MissingEnvVar { var: &'static str },
|
|
|
|
}
|
|
|
|
|
2024-08-09 12:27:31 +02:00
|
|
|
/// 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
|
2024-08-09 12:55:20 +02:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct NotHtmxPredicate;
|
|
|
|
impl<T> Predicate<Request<T>> for NotHtmxPredicate {
|
|
|
|
fn check(&mut self, req: &Request<T>) -> bool {
|
|
|
|
!(req.headers().contains_key("hx-request"))
|
|
|
|
}
|
2024-08-08 22:19:24 +02:00
|
|
|
}
|
2024-07-23 20:08:45 +02:00
|
|
|
|
2024-08-09 12:27:31 +02:00
|
|
|
const DEFAULT_LISTENER: &str = "localhost:3000";
|
2024-08-14 10:40:41 +02:00
|
|
|
async fn get_tcp_listener() -> Result<TcpListener, io::Error> {
|
2024-08-09 12:27:31 +02:00
|
|
|
let mut listenfd = ListenFd::from_env();
|
|
|
|
|
2024-08-14 10:40:41 +02:00
|
|
|
match listenfd.take_tcp_listener(0)? {
|
2024-08-09 12:27:31 +02:00
|
|
|
// if we are given a tcp listener on listen fd 0, we use that one
|
|
|
|
Some(listener) => {
|
2024-08-14 10:40:41 +02:00
|
|
|
listener.set_nonblocking(true)?;
|
|
|
|
Ok(TcpListener::from_std(listener)?)
|
2024-08-09 12:27:31 +02:00
|
|
|
}
|
|
|
|
// otherwise fall back to local listening
|
2024-08-14 10:40:41 +02:00
|
|
|
None => Ok(TcpListener::bind(DEFAULT_LISTENER).await?),
|
2024-08-09 12:27:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-14 10:40:41 +02:00
|
|
|
fn get_livereload_layer(
|
|
|
|
templates_path: &Path,
|
|
|
|
) -> Result<LiveReloadLayer<NotHtmxPredicate>, notify::Error> {
|
2024-08-08 22:19:24 +02:00
|
|
|
let livereload = LiveReloadLayer::new();
|
|
|
|
let reloader = livereload.reloader();
|
2024-08-14 10:40:41 +02:00
|
|
|
let mut watcher = notify::recommended_watcher(move |_| reloader.reload())?;
|
|
|
|
watcher.watch(templates_path, notify::RecursiveMode::Recursive)?;
|
|
|
|
Ok(livereload.request_predicate::<Body, NotHtmxPredicate>(NotHtmxPredicate))
|
2024-08-09 12:55:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
2024-08-14 10:40:41 +02:00
|
|
|
async fn main() -> Result<(), AppError> {
|
|
|
|
let manifest_dir = env::var("CARGO_MANIFEST_DIR").map_err(|_| AppError::MissingEnvVar {
|
|
|
|
var: "CARGO_MANIFEST_DIR",
|
|
|
|
})?;
|
2024-08-09 12:55:20 +02:00
|
|
|
let assets_path = Path::new(&manifest_dir).join("assets");
|
|
|
|
let templates_path = Path::new(&manifest_dir).join("templates");
|
2024-07-23 20:08:45 +02:00
|
|
|
|
2024-08-14 10:40:41 +02:00
|
|
|
let livereload_layer =
|
|
|
|
get_livereload_layer(&templates_path).map_err(AppError::NotifyWatcher)?;
|
2024-08-09 12:55:20 +02:00
|
|
|
let router = get_router(assets_path.as_path()).layer(livereload_layer);
|
2024-08-08 22:14:11 +02:00
|
|
|
|
2024-08-14 10:40:41 +02:00
|
|
|
let listener: TcpListener = get_tcp_listener().await.map_err(AppError::TCPListener)?;
|
|
|
|
let local_addr = listener.local_addr().map_err(AppError::TCPListener)?;
|
|
|
|
println!("Listening on: http://{}", local_addr);
|
2024-08-08 22:14:11 +02:00
|
|
|
|
|
|
|
// Run the server with the router
|
2024-08-14 10:40:41 +02:00
|
|
|
axum::serve(listener, router.into_make_service()).await?;
|
|
|
|
Ok(())
|
2024-07-23 20:08:45 +02:00
|
|
|
}
|