feat: add auto-reload on development environment
This commit is contained in:
parent
b10fc30984
commit
4a54846f47
5
.ignore
Normal file
5
.ignore
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# Ignorer les fichiers dont ne dépent pas la compilation
|
||||||
|
*.md
|
||||||
|
tailwind.config.js
|
||||||
|
*.example
|
||||||
|
scripts
|
12
Cargo.lock
generated
12
Cargo.lock
generated
@ -69,6 +69,7 @@ dependencies = [
|
|||||||
"askama",
|
"askama",
|
||||||
"askama_axum",
|
"askama_axum",
|
||||||
"axum",
|
"axum",
|
||||||
|
"listenfd",
|
||||||
"serde",
|
"serde",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tower-http",
|
"tower-http",
|
||||||
@ -1717,6 +1718,17 @@ dependencies = [
|
|||||||
"libc",
|
"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]]
|
[[package]]
|
||||||
name = "lock_api"
|
name = "lock_api"
|
||||||
version = "0.4.12"
|
version = "0.4.12"
|
||||||
|
@ -7,6 +7,7 @@ edition = "2021"
|
|||||||
askama = "0.12.1"
|
askama = "0.12.1"
|
||||||
askama_axum = "0.4.0"
|
askama_axum = "0.4.0"
|
||||||
axum = "0.7.5"
|
axum = "0.7.5"
|
||||||
|
listenfd = "1.0.1"
|
||||||
serde = { version = "1.0.204", features = ["derive"] }
|
serde = { version = "1.0.204", features = ["derive"] }
|
||||||
tokio = { version = "1.39.1", features = ["macros", "rt-multi-thread"] }
|
tokio = { version = "1.39.1", features = ["macros", "rt-multi-thread"] }
|
||||||
tower-http = { version = "0.5.2", features = ["fs"] }
|
tower-http = { version = "0.5.2", features = ["fs"] }
|
||||||
|
@ -13,3 +13,17 @@
|
|||||||
```bash
|
```bash
|
||||||
cargo run --bin app
|
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'
|
||||||
|
```
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
use ::app::get_router;
|
use ::app::get_router;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
use listenfd::ListenFd;
|
||||||
|
use tokio::net::TcpListener;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
@ -8,8 +10,19 @@ async fn main() {
|
|||||||
let assets_path = Path::new(&manifest_dir).join("assets");
|
let assets_path = Path::new(&manifest_dir).join("assets");
|
||||||
let router = get_router(assets_path.as_path());
|
let router = get_router(assets_path.as_path());
|
||||||
|
|
||||||
// TODO: select port based on available port (or ask in CLI)
|
let mut listenfd = ListenFd::from_env();
|
||||||
let listener = tokio::net::TcpListener::bind("localhost:3000").await.unwrap();
|
let listener = match listenfd.take_tcp_listener(0).unwrap() {
|
||||||
println!("Listening on: http://{}", listener.local_addr().unwrap());
|
// if we are given a tcp listener on listen fd 0, we use that one
|
||||||
axum::serve(listener, router).await.unwrap();
|
Some(listener) => {
|
||||||
|
listener.set_nonblocking(true).unwrap();
|
||||||
|
TcpListener::from_std(listener).unwrap()
|
||||||
|
}
|
||||||
|
// otherwise fall back to local listening
|
||||||
|
None => TcpListener::bind("127.0.0.1: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();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user