feat: Add auto-reload on development environment

This commit is contained in:
Simon C
2024-08-08 22:14:11 +02:00
parent 668a91941b
commit 1ae80c161f
5 changed files with 49 additions and 5 deletions

View File

@ -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();
}