Skip to content

Commit

Permalink
add file watcher for dev
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Nov 29, 2024
1 parent 05e66a5 commit 2d83e86
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 3 deletions.
111 changes: 111 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion kitsune/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ eula = false

[dependencies]
athena.workspace = true
arc-swap = "1.7.1"
argon2 = { version = "0.5.3", features = ["std"] }
async-trait = "0.1.83"
axum = { version = "0.7.9", features = ["macros", "multipart"] }
Expand Down Expand Up @@ -70,6 +71,7 @@ mimalloc = "0.1.43"
mime = "0.3.17"
mime_guess = { version = "2.0.5", default-features = false }
minijinja.workspace = true
notify = "7.0.0"
oxide-auth = "0.6.1"
oxide-auth-async = "0.2.1"
oxide-auth-axum = "0.5.0"
Expand Down Expand Up @@ -120,7 +122,6 @@ kitsune-mastodon = { workspace = true, optional = true }

# "oidc" feature
kitsune-oidc = { workspace = true, optional = true }
arc-swap = "1.7.1"

[dev-dependencies]
kitsune-http-client.workspace = true
Expand Down
27 changes: 25 additions & 2 deletions kitsune/src/template.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use arc_swap::ArcSwapAny;
use core::str;
use notify::Watcher;
use rust_embed::RustEmbed;
use std::sync::OnceLock;
use std::{mem::ManuallyDrop, path::Path, sync::OnceLock};
use triomphe::Arc;

static ENVIRONMENT: OnceLock<ArcSwapAny<Arc<minijinja::Environment<'static>>>> = OnceLock::new();
Expand All @@ -28,14 +29,36 @@ fn init_environment() -> minijinja::Environment<'static> {
environment
}

fn spawn_watcher() {
let watcher = notify::recommended_watcher(|event: notify::Result<notify::Event>| {
if event.is_err() {
return;
}

ENVIRONMENT
.get()
.unwrap()
.store(Arc::new(init_environment()));
})
.unwrap();

let mut watcher = ManuallyDrop::new(watcher);
let template_dir = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/templates"));

watcher
.watch(template_dir, notify::RecursiveMode::Recursive)
.unwrap();
}

#[track_caller]
pub fn render<S>(name: &str, ctx: S) -> Option<String>
where
S: serde::Serialize,
{
let handle = ENVIRONMENT
.get_or_init(|| {
// ToDo: Spawn watcher on the path which replaces the environment through the arc-swap
#[cfg(debug_assertions)]
spawn_watcher();
ArcSwapAny::new(Arc::new(init_environment()))
})
.load_full();
Expand Down

0 comments on commit 2d83e86

Please sign in to comment.