This commit is contained in:
Daniel Flanagan 2024-05-06 15:39:21 -05:00
parent e2bd82d9ac
commit 28f1f9adb8
2 changed files with 35 additions and 33 deletions

View file

@ -1,50 +1,57 @@
use axum::{serve, Router};
use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher};
use std::{ use std::{
cell::OnceCell,
path::{Path, PathBuf}, path::{Path, PathBuf},
str::FromStr, str::FromStr,
sync::OnceLock,
}; };
use tokio::sync::mpsc::channel; use tokio::sync::mpsc::channel;
use tower_http::services::ServeDir;
use tower_livereload::{LiveReloadLayer, Reloader};
pub use tracing::{debug, error, info, warn};
use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher}; type Berr = Box<dyn std::error::Error>;
type Besult<T> = Result<T, Berr>;
mod observe { mod observe {
use tracing::level_filters::LevelFilter;
use tracing_subscriber::EnvFilter;
pub fn setup_logging() { pub fn setup_logging() {
color_eyre::install().expect("Failed to install color_eyre"); color_eyre::install().expect("Failed to install color_eyre");
let filter = EnvFilter::builder() let filter = tracing_subscriber::EnvFilter::builder()
.with_default_directive(LevelFilter::TRACE.into()) .with_default_directive(<tracing_subscriber::filter::Directive>::from(
.parse_lossy("info,chatbot=trace"); tracing::level_filters::LevelFilter::TRACE,
))
.parse_lossy("info,lyrs=trace");
tracing_subscriber::fmt().with_env_filter(filter).init(); tracing_subscriber::fmt().with_env_filter(filter).init();
} }
} }
mod prelude { fn static_file_dir() -> &'static PathBuf {
#![allow(unused_imports)] static STATIC_FILE_DIR: OnceLock<PathBuf> = OnceLock::new();
pub use tracing::{debug, error, info, warn}; STATIC_FILE_DIR.get_or_init(|| PathBuf::from_str("static").unwrap())
}
use axum::{serve, Router};
use tower_http::services::ServeDir;
use tower_livereload::LiveReloadLayer;
use crate::prelude::*;
const LIVE_RELOADING: bool = true;
const STATIC_FILE_PATH: OnceCell<PathBuf> = OnceCell::new();
fn static_file_dir() -> &'static Path {
STATIC_FILE_PATH.get_or_init(|| PathBuf::from_str("static").unwrap())
} }
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Besult<()> {
// load configuration? // load configuration?
observe::setup_logging(); observe::setup_logging();
let rl_layer = live_reload_layer()?;
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
info!("Listening on {listener:#?}");
Ok(serve(listener, static_files()?.layer(rl_layer)).await?)
}
fn live_reload_layer() -> Besult<LiveReloadLayer> {
let rl_layer = LiveReloadLayer::new();
let rl = rl_layer.reloader();
static_file_watcher(rl)?;
Ok(rl_layer)
}
fn static_file_watcher(reloader: Reloader) -> Result<(), Box<dyn std::error::Error>> {
info!("Creating async watcher..."); info!("Creating async watcher...");
let (tx, mut rx) = channel(1); let (tx, mut rx) = channel(1);
@ -63,9 +70,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
watcher.watch(static_file_dir(), RecursiveMode::Recursive)?; watcher.watch(static_file_dir(), RecursiveMode::Recursive)?;
let rl_layer = LiveReloadLayer::new();
let rl = rl_layer.reloader();
tokio::spawn(async move { tokio::spawn(async move {
info!("Recieving..."); info!("Recieving...");
while let Some(res) = rx.recv().await { while let Some(res) = rx.recv().await {
@ -73,16 +77,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
match res { match res {
Ok(event) => { Ok(event) => {
info!("fs event: {event:#?}"); info!("fs event: {event:#?}");
rl.reload() reloader.reload()
} }
Err(e) => println!("watch error: {:?}", e), Err(e) => println!("watch error: {:?}", e),
} }
} }
}); });
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); Ok(())
info!("Listening on {listener:#?}");
Ok(serve(listener, static_files()?.layer(rl_layer)).await?)
} }
fn static_files() -> Result<Router, notify::Error> { fn static_files() -> Result<Router, notify::Error> {

View file

@ -1,2 +1,2 @@
<!DOCTYPE html> <!DOCTYPE html>
Sup Dawgyyyyyyyyy Sup Dawgy