1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use std::error::Error;
use std::path::{Path, PathBuf};
use crate::engine;
use normpath::PathExt;
use tera::Tera;
pub(crate) type Callback =
Box<dyn Fn(&mut Tera) -> Result<(), Box<dyn Error>> + Send + Sync + 'static>;
pub(crate) struct Context {
/// The root of the template directory.
pub root: PathBuf,
/// The initialized templating engine.
pub tera: Tera,
}
pub(crate) use self::manager::ContextManager;
impl Context {
/// Load all of the templates at `root`, initialize them using the relevant
/// template engine, and store all of the initialized state in a `Context`
/// structure, which is returned if all goes well.
pub fn initialize(root: &Path, callback: &Callback) -> Option<Context> {
let root = match root.normalize() {
Ok(root) => root.into_path_buf(),
Err(e) => {
error!("Invalid template directory '{}': {}.", root.display(), e);
return None;
}
};
let mut tera = engine::init();
if let Err(reason) = callback(&mut tera) {
error_!("Template customization callback failed.");
error_!("{}", reason);
engine::log_error(&*reason);
return None;
}
engine::load(&mut tera, &root)?;
Some(Context { root, tera })
}
}
#[cfg(not(debug_assertions))]
mod manager {
use super::Context;
use std::ops::Deref;
/// Wraps a Context. With `cfg(debug_assertions)` active, this structure
/// additionally provides a method to reload the context at runtime.
pub(crate) struct ContextManager(Context);
impl ContextManager {
pub fn new(ctxt: Context) -> ContextManager {
ContextManager(ctxt)
}
pub fn context<'a>(&'a self) -> impl Deref<Target = Context> + 'a {
&self.0
}
pub fn is_reloading(&self) -> bool {
false
}
}
}
#[cfg(debug_assertions)]
mod manager {
use std::ops::{Deref, DerefMut};
use std::sync::mpsc::{Receiver, channel};
use std::sync::{Mutex, RwLock};
use notify::{Error, Event, RecommendedWatcher, RecursiveMode, Watcher, recommended_watcher};
use super::Context;
use crate::engine;
/// A filesystem watcher paired with the receive queue for its events.
type Watched = (RecommendedWatcher, Mutex<Receiver<Result<Event, Error>>>);
/// Wraps a Context. With `cfg(debug_assertions)` active, this structure
/// additionally provides a method to reload the context at runtime.
pub(crate) struct ContextManager {
/// The current template context, inside an RwLock so it can be updated.
context: RwLock<Context>,
/// A filesystem watcher and the receive queue for its events.
watcher: Option<Watched>,
}
impl ContextManager {
pub fn new(ctxt: Context) -> ContextManager {
let (tx, rx) = channel();
let watcher = recommended_watcher(tx).and_then(|mut watcher| {
watcher.watch(&ctxt.root.canonicalize()?, RecursiveMode::Recursive)?;
Ok(watcher)
});
let watcher = match watcher {
Ok(watcher) => Some((watcher, Mutex::new(rx))),
Err(e) => {
warn!("Failed to enable live template reloading: {e}");
warn_!("Live template reloading is unavailable.");
None
}
};
ContextManager {
watcher,
context: RwLock::new(ctxt),
}
}
pub fn context(&self) -> impl Deref<Target = Context> + '_ {
self.context.read().unwrap()
}
pub fn is_reloading(&self) -> bool {
self.watcher.is_some()
}
fn context_mut(&self) -> impl DerefMut<Target = Context> + '_ {
self.context.write().unwrap()
}
/// Checks whether any template files have changed on disk. If there
/// have been changes since the last reload, all templates are
/// reloaded from disk.
pub fn reload_if_needed(&self) {
// Access events don't change templates, and reloading generates
// them by reading every template, so ignore them to avoid reloading
// on every request. Since notify v7, inotify always reports opens.
// TODO: use `Config::with_event_kinds(EventKindMask::CORE)` once
// notify v9 is released, so these events aren't watched at all.
let templates_changes = self.watcher.as_ref().map(|(_, rx)| {
rx.lock()
.expect("fsevents lock")
.try_iter()
.filter(|event| !event.as_ref().is_ok_and(|e| e.kind.is_access()))
.count()
> 0
});
if let Some(true) = templates_changes {
debug_!("Change detected: reloading templates.");
if engine::reload(&mut self.context_mut().tera).is_none() {
warn_!("An error occurred while reloading templates.");
warn_!("Existing templates will remain active.");
};
}
}
}
}