Add (semi) live reloading for debug server

This commit is contained in:
2023-06-01 00:59:46 -04:00
parent e501c15c66
commit 74f353b894
3 changed files with 135 additions and 1 deletions

View File

@@ -3,9 +3,11 @@ use axum::{
Router,
};
use dotenvy::dotenv;
use notify::Watcher;
use sqlx::postgres::PgPoolOptions;
use std::env;
use std::{env, path::Path};
use tower::ServiceBuilder;
use tower_livereload::LiveReloadLayer;
use tower_http::trace::TraceLayer;
use tracing::debug;
@@ -35,6 +37,12 @@ async fn main() -> anyhow::Result<()> {
.with_state(pool)
.layer(ServiceBuilder::new().layer(TraceLayer::new_for_http()));
let livereload = LiveReloadLayer::new();
let reloader = livereload.reloader();
let mut watcher = notify::recommended_watcher(move |_| reloader.reload())?;
watcher.watch(Path::new("target/debug/crawlnicle"), notify::RecursiveMode::Recursive)?;
let app = app.layer(livereload);
let addr = (env::var("HOST")? + ":" + &env::var("PORT")?).parse()?;
debug!("listening on {}", addr);
axum::Server::bind(&addr)