Trying to setup file watching and stuff

This commit is contained in:
Daniel Flanagan 2024-05-05 11:22:54 -05:00
parent 675ce6a8be
commit 240ee00d6b
8 changed files with 1859 additions and 5 deletions

1756
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -6,3 +6,15 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
axum = { version = "0.7.5", features = ["macros", "tokio"] }
color-eyre = "0.6.3"
config = "0.14.0"
futures = "0.3.30"
notify = "6.1.1"
redact = { version = "0.1.9", features = ["serde"] }
tokio = { version = "1.37.0", features = ["full"] }
tower = "0.4.13"
tower-http = { version = "0.5.2", features = ["fs"] }
tower-livereload = "0.9.2"
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }

View file

@ -26,6 +26,7 @@
rust-analyzer
lldb
pkg-config
inotify-tools
];
};

View file

@ -1,3 +0,0 @@
<script>
console.log("Hello, world!")
</script

View file

@ -1,3 +1,75 @@
fn main() {
println!("Hello, world!");
use std::{
cell::OnceCell,
path::{Path, PathBuf},
str::FromStr,
};
use tokio::sync::mpsc::channel;
use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher};
mod observe;
mod prelude;
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]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// load configuration?
observe::setup_logging();
info!("Creating async watcher...");
let (tx, mut rx) = channel(1);
info!("Creating watcher...");
let mut watcher = RecommendedWatcher::new(
move |res| {
info!("Res from watcher: {res:#?}");
futures::executor::block_on(async {
tx.send(res).await.unwrap();
})
},
Config::default(),
)?;
info!("Created watcher");
watcher.watch(static_file_dir(), RecursiveMode::Recursive)?;
let rl_layer = LiveReloadLayer::new();
let rl = rl_layer.reloader();
tokio::spawn(async move {
info!("Recieving...");
while let Some(res) = rx.recv().await {
info!("Recieved! {res:#?}");
match res {
Ok(event) => {
info!("fs event: {event:#?}");
rl.reload()
}
Err(e) => println!("watch error: {:?}", e),
}
}
});
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 static_files() -> Result<Router, notify::Error> {
let static_file_path = Path::new("./static/");
// serve the file in the "assets" directory under `/assets`
info!("Starting static file server...");
Ok(Router::new().nest_service("/static", ServeDir::new(static_file_path)))
}

12
src/observe.rs Normal file
View file

@ -0,0 +1,12 @@
use tracing::level_filters::LevelFilter;
use tracing_subscriber::EnvFilter;
pub fn setup_logging() {
color_eyre::install().expect("Failed to install color_eyre");
let filter = EnvFilter::builder()
.with_default_directive(LevelFilter::TRACE.into())
.parse_lossy("info,chatbot=trace");
tracing_subscriber::fmt().with_env_filter(filter).init();
}

2
src/prelude.rs Normal file
View file

@ -0,0 +1,2 @@
#![allow(unused_imports)]
pub use tracing::{debug, error, info, warn};

2
static/index.html Normal file
View file

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