From 1ae80c161f6cee193ec3659ee0b1bf9add8315d0 Mon Sep 17 00:00:00 2001 From: Simon C Date: Thu, 8 Aug 2024 22:14:11 +0200 Subject: [PATCH] feat: Add auto-reload on development environment --- .ignore | 5 +++++ Cargo.lock | 12 ++++++++++++ crates/app/Cargo.toml | 2 +- crates/app/README.md | 14 ++++++++++++++ crates/app/src/main.rs | 21 +++++++++++++++++---- 5 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 .ignore diff --git a/.ignore b/.ignore new file mode 100644 index 0000000..4522f2d --- /dev/null +++ b/.ignore @@ -0,0 +1,5 @@ +# Ignorer les fichiers dont ne dépent pas la compilation +*.md +tailwind.config.js +*.example +scripts diff --git a/Cargo.lock b/Cargo.lock index eadcf9e..9791f9f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -69,6 +69,7 @@ dependencies = [ "askama", "askama_axum", "axum", + "listenfd", "serde", "tokio", "tower-http", @@ -1717,6 +1718,17 @@ dependencies = [ "libc", ] +[[package]] +name = "listenfd" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0500463acd96259d219abb05dc57e5a076ef04b2db9a2112846929b5f174c96" +dependencies = [ + "libc", + "uuid", + "winapi", +] + [[package]] name = "lock_api" version = "0.4.12" diff --git a/crates/app/Cargo.toml b/crates/app/Cargo.toml index 23c7568..20024c8 100644 --- a/crates/app/Cargo.toml +++ b/crates/app/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" askama = "0.12.1" askama_axum = "0.4.0" axum = "0.7.5" +listenfd = "1.0.1" serde = { version = "1.0.204", features = ["derive"] } tokio = { version = "1.39.1", features = ["macros", "rt-multi-thread"] } tower-http = { version = "0.5.2", features = ["fs"] } - diff --git a/crates/app/README.md b/crates/app/README.md index e728e9a..050bb7f 100644 --- a/crates/app/README.md +++ b/crates/app/README.md @@ -13,3 +13,17 @@ ```bash cargo run --bin app ``` + +## L'auto-reload + +Pour permettre au projet de s'auto-recharger lors du développement, nous avons besoin de 2 librairies : + +```bash +cargo install cargo-watch systemfd +``` + +Pour recompiler automatiquement le serveur lors de changement : + +```bash +systemfd --no-pid -s http::3000 -- cargo watch -x 'run --bin app' +``` diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index b409b11..333d905 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -1,6 +1,8 @@ use ::app::get_router; +use listenfd::ListenFd; use std::env; use std::path::Path; +use tokio::net::TcpListener; #[tokio::main] async fn main() { @@ -8,10 +10,21 @@ async fn main() { let assets_path = Path::new(&manifest_dir).join("assets"); let router = get_router(assets_path.as_path()); - // TODO: select port based on available port (or ask in CLI) - let listener = tokio::net::TcpListener::bind("localhost:3000") + let mut listenfd = ListenFd::from_env(); + let listener = match listenfd.take_tcp_listener(0).unwrap() { + // if we are given a tcp listener on listen fd 0, we use that one + Some(listener) => { + listener.set_nonblocking(true).unwrap(); + TcpListener::from_std(listener).unwrap() + } + // otherwise fall back to local listening + None => TcpListener::bind("localhost:3000").await.unwrap(), + }; + + println!("Listening on: http://{}", listener.local_addr().unwrap()); + + // Run the server with the router + axum::serve(listener, router.into_make_service()) .await .unwrap(); - println!("Listening on: http://{}", listener.local_addr().unwrap()); - axum::serve(listener, router).await.unwrap(); }