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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use templates::{DEFAULT_TEMPLATE_DIR, Context, Engines};
use rocket::Rocket;
use rocket::config::ConfigError;
use rocket::fairing::{Fairing, Info, Kind};
crate use self::context::ContextManager;
#[cfg(not(debug_assertions))]
mod context {
use std::ops::Deref;
use templates::Context;
crate struct ContextManager(Context);
impl ContextManager {
crate fn new(ctxt: Context) -> ContextManager {
ContextManager(ctxt)
}
crate fn context<'a>(&'a self) -> impl Deref<Target=Context> + 'a {
&self.0
}
crate fn is_reloading(&self) -> bool {
false
}
}
}
#[cfg(debug_assertions)]
mod context {
extern crate notify;
use std::ops::{Deref, DerefMut};
use std::sync::{RwLock, Mutex};
use std::sync::mpsc::{channel, Receiver};
use templates::{Context, Engines};
use self::notify::{raw_watcher, RawEvent, RecommendedWatcher, RecursiveMode, Watcher};
crate struct ContextManager {
context: RwLock<Context>,
watcher: Option<Mutex<(RecommendedWatcher, Receiver<RawEvent>)>>,
}
impl ContextManager {
crate fn new(ctxt: Context) -> ContextManager {
let (tx, rx) = channel();
let watcher = raw_watcher(tx).and_then(|mut watcher| {
watcher.watch(ctxt.root.canonicalize()?, RecursiveMode::Recursive)?;
Ok(watcher)
});
let watcher = match watcher {
Ok(watcher) => Some(Mutex::new((watcher, rx))),
Err(e) => {
warn!("Failed to enable live template reloading: {}", e);
debug_!("Reload error: {:?}", e);
warn_!("Live template reloading is unavailable.");
None
}
};
ContextManager {
watcher,
context: RwLock::new(ctxt),
}
}
crate fn context<'a>(&'a self) -> impl Deref<Target=Context> + 'a {
self.context.read().unwrap()
}
crate fn is_reloading(&self) -> bool {
self.watcher.is_some()
}
fn context_mut<'a>(&'a self) -> impl DerefMut<Target=Context> + 'a {
self.context.write().unwrap()
}
crate fn reload_if_needed<F: Fn(&mut Engines)>(&self, custom_callback: F) {
self.watcher.as_ref().map(|w| {
let rx_lock = w.lock().expect("receive queue lock");
let mut changed = false;
while let Ok(_) = rx_lock.1.try_recv() {
changed = true;
}
if changed {
info_!("Change detected: reloading templates.");
let mut ctxt = self.context_mut();
if let Some(mut new_ctxt) = Context::initialize(ctxt.root.clone()) {
custom_callback(&mut new_ctxt.engines);
*ctxt = new_ctxt;
} else {
warn_!("An error occurred while reloading templates.");
warn_!("The previous templates will remain active.");
};
}
});
}
}
}
pub struct TemplateFairing {
crate custom_callback: Box<Fn(&mut Engines) + Send + Sync + 'static>,
}
impl Fairing for TemplateFairing {
fn info(&self) -> Info {
Info {
name: "Templates",
#[cfg(debug_assertions)]
kind: Kind::Attach | Kind::Request,
#[cfg(not(debug_assertions))]
kind: Kind::Attach,
}
}
fn on_attach(&self, rocket: Rocket) -> Result<Rocket, Rocket> {
let mut template_root = rocket.config().root_relative(DEFAULT_TEMPLATE_DIR);
match rocket.config().get_str("template_dir") {
Ok(dir) => template_root = rocket.config().root_relative(dir),
Err(ConfigError::Missing(_)) => { }
Err(e) => {
e.pretty_print();
warn_!("Using default templates directory '{:?}'", template_root);
}
};
match Context::initialize(template_root) {
Some(mut ctxt) => {
(self.custom_callback)(&mut ctxt.engines);
Ok(rocket.manage(ContextManager::new(ctxt)))
}
None => Err(rocket),
}
}
#[cfg(debug_assertions)]
fn on_request(&self, req: &mut ::rocket::Request, _data: &::rocket::Data) {
let cm = req.guard::<::rocket::State<ContextManager>>()
.expect("Template ContextManager registered in on_attach");
cm.reload_if_needed(&*self.custom_callback);
}
}