Skip to content

Commit 1fbcc19

Browse files
committed
Added tera-templates dir and made watching good
1 parent 3d469fe commit 1fbcc19

File tree

4 files changed

+101
-12
lines changed

4 files changed

+101
-12
lines changed

Diff for: Cargo.lock

+76
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ tera = { version = "1.3.0", features = ["builtins"] }
5757

5858
# Template hot-reloading
5959
arc-swap = "0.4.6"
60+
notify = "4.0.15"
6061

6162
[target.'cfg(not(windows))'.dependencies]
6263
libc = "0.2"

Diff for: src/web/page/templates.rs

+24-12
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,31 @@ impl TemplateData {
4242
}
4343

4444
pub fn start_template_reloading() {
45-
use std::{sync::Arc, thread, time::Duration};
46-
47-
thread::spawn(|| loop {
48-
match load_templates() {
49-
Ok(templates) => {
50-
log::info!("Reloaded templates");
51-
TEMPLATE_DATA.templates.swap(Arc::new(templates));
52-
thread::sleep(Duration::from_secs(10));
53-
}
45+
use notify::{watcher, RecursiveMode, Watcher};
46+
use std::{
47+
sync::{mpsc::channel, Arc},
48+
thread,
49+
time::Duration,
50+
};
51+
52+
let (tx, rx) = channel();
53+
let mut watcher = watcher(tx, Duration::from_secs(2)).unwrap();
54+
55+
watcher
56+
.watch("tera-templates", RecursiveMode::Recursive)
57+
.unwrap();
58+
59+
thread::spawn(move || {
60+
let _watcher = watcher;
61+
62+
while rx.recv().is_ok() {
63+
match load_templates() {
64+
Ok(templates) => {
65+
log::info!("Reloaded templates");
66+
super::TEMPLATE_DATA.templates.swap(Arc::new(templates));
67+
}
5468

55-
Err(err) => {
56-
log::info!("Error Loading Templates:\n{}", err);
57-
thread::sleep(Duration::from_secs(5));
69+
Err(err) => log::error!("Error reloading templates: {:?}", err),
5870
}
5971
}
6072
});

Diff for: tera-templates/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)