1 | var path = require('path');
|
2 | var fs = require('fs');
|
3 | var http = require('http');
|
4 |
|
5 | var util = require("../util/util");
|
6 |
|
7 | var dust = require("dustjs-linkedin");
|
8 | require("dustjs-helpers");
|
9 |
|
10 |
|
11 |
|
12 | dust.config.cache = false;
|
13 |
|
14 |
|
15 | var TEMPLATE_CACHE = {};
|
16 |
|
17 | if (process.env.CLOUDCMS_APPSERVER_MODE === "production") {
|
18 | dust.debugLevel = "INFO";
|
19 | } else {
|
20 | dust.debugLevel = "DEBUG";
|
21 | dust.config.cache = false;
|
22 | }
|
23 |
|
24 | if (process.env.CLOUDCMS_APPSERVER_MODE !== "production") {
|
25 | dust.config.whitespace = true;
|
26 | }
|
27 |
|
28 | if (process.env.DUST_DEBUG_LEVEL) {
|
29 | dust.debugLevel = (process.env.DUST_DEBUG_LEVEL + "").toUpperCase();
|
30 | }
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | dust.isThenable = function(elem) {
|
39 | return elem &&
|
40 | typeof elem === 'object' &&
|
41 | typeof elem.then === 'function' && !elem.objectType;
|
42 | };
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | var loadTemplate = dust.onLoad = function(templatePath, options, callback)
|
53 | {
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | var store = options.store;
|
62 |
|
63 | store.existsFile(templatePath, function(exists) {
|
64 |
|
65 | if (!exists) {
|
66 | return callback({
|
67 | "message": "Dust cannot find file: " + templatePath
|
68 | });
|
69 | }
|
70 |
|
71 | store.readFile(templatePath, function(err, data) {
|
72 |
|
73 | if (err) {
|
74 | return callback(err);
|
75 | }
|
76 |
|
77 | callback(null, "" + data);
|
78 | });
|
79 | });
|
80 | };
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | var exports = module.exports;
|
86 |
|
87 | exports.getDust = function()
|
88 | {
|
89 | return dust;
|
90 | };
|
91 |
|
92 | var populateContext = function(req, context, model, templateFilePath)
|
93 | {
|
94 | if (model)
|
95 | {
|
96 | for (var k in model)
|
97 | {
|
98 | context[k] = model[k];
|
99 | }
|
100 | }
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | if (req.user)
|
106 | {
|
107 | context.user = req.user;
|
108 | context.userId = req.user.id;
|
109 | context.userName = req.user.name;
|
110 | }
|
111 |
|
112 |
|
113 | var qs = {};
|
114 | if (req.query)
|
115 | {
|
116 | for (var name in req.query)
|
117 | {
|
118 | var value = req.query[name];
|
119 | if (value)
|
120 | {
|
121 | qs[name] = value;
|
122 | }
|
123 | }
|
124 | }
|
125 | if (!context.request) {
|
126 | context.request = {};
|
127 | }
|
128 | context.request.uri = req.originalUrl;
|
129 | context.request.path = req.path;
|
130 | context.request.qs = qs;
|
131 |
|
132 | context.gitana = req.gitana;
|
133 | context.templateFilePaths = [templateFilePath];
|
134 | context.req = req;
|
135 | context.messages = {};
|
136 |
|
137 |
|
138 | if (req.flash)
|
139 | {
|
140 | var info = req.flash("info");
|
141 | if (info)
|
142 | {
|
143 | context.messages.info = info;
|
144 | }
|
145 | }
|
146 |
|
147 | if (req.helpers)
|
148 | {
|
149 | context.helpers = req.helpers;
|
150 | }
|
151 |
|
152 |
|
153 |
|
154 | context["__tracker"] = {
|
155 | "requires": {},
|
156 | "produces": {}
|
157 | };
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | context["fragmentIdGenerator"] = function(url) {
|
166 | var counter = 0;
|
167 | return function() {
|
168 | return util.hashSignature("fragment-" + url + "-" + (++counter));
|
169 | };
|
170 | }(req.url);
|
171 | };
|
172 |
|
173 | exports.invalidateCacheForApp = function(applicationId)
|
174 | {
|
175 | console.log("Invalidating dust cache for application: " + applicationId);
|
176 |
|
177 | var prefix = applicationId + ":";
|
178 |
|
179 | var badKeys = [];
|
180 | for (var k in TEMPLATE_CACHE)
|
181 | {
|
182 | if (k.indexOf(prefix) === 0) {
|
183 | badKeys.push(k);
|
184 | }
|
185 | }
|
186 | for (var i = 0; i < badKeys.length; i++)
|
187 | {
|
188 | console.log("Removing dust cache key: " + badKeys[i]);
|
189 | delete TEMPLATE_CACHE[badKeys[i]];
|
190 | }
|
191 | };
|
192 |
|
193 | exports.execute = function(req, store, filePath, model, callback)
|
194 | {
|
195 | if (typeof(model) === "function")
|
196 | {
|
197 | callback = model;
|
198 | model = {};
|
199 | }
|
200 |
|
201 | ensureInit(store);
|
202 |
|
203 | var templatePath = filePath.split(path.sep).join("/");
|
204 |
|
205 |
|
206 | var contextObject = {};
|
207 | populateContext(req, contextObject, model, templatePath);
|
208 | var context = dust.context(contextObject, {
|
209 | "req": req,
|
210 | "store": store,
|
211 | "log": dust.log,
|
212 | "dust": dust
|
213 | });
|
214 |
|
215 |
|
216 | var tracker = context.get("__tracker");
|
217 |
|
218 | var executeTemplate = function(template, templatePath, context, callback)
|
219 | {
|
220 |
|
221 | var t1 = new Date().getTime();
|
222 | dust.render(template, context, function(err, out) {
|
223 | var t2 = new Date().getTime();
|
224 |
|
225 | if (err)
|
226 | {
|
227 | req.log("An error was caught while rendering dust template: " + templatePath + ", error: " + JSON.stringify(err, null, " "));
|
228 | }
|
229 |
|
230 |
|
231 | context.req = null;
|
232 | context.gitana = null;
|
233 | context.user = null;
|
234 |
|
235 | var dependencies = {
|
236 | "requires": tracker.requires,
|
237 | "produces": tracker.produces
|
238 | };
|
239 |
|
240 | var stats = {
|
241 | "dustExecutionTime": (t2 - t1)
|
242 | };
|
243 |
|
244 |
|
245 | callback(err, out, dependencies, stats);
|
246 | });
|
247 | };
|
248 |
|
249 |
|
250 | var templateCacheKey = store.id + "_" + templatePath;
|
251 | var template = TEMPLATE_CACHE[templateCacheKey];
|
252 | if (template)
|
253 | {
|
254 |
|
255 | return executeTemplate(template, templatePath, context, callback);
|
256 | }
|
257 |
|
258 |
|
259 |
|
260 | loadTemplate(templatePath, {
|
261 | "store": store
|
262 | }, function(err, text) {
|
263 |
|
264 | if (err) {
|
265 | return callback(err);
|
266 | }
|
267 |
|
268 |
|
269 | try {
|
270 |
|
271 | template = dust.loadSource(dust.compile(text));
|
272 | TEMPLATE_CACHE[templateCacheKey] = template;
|
273 | } catch (e) {
|
274 | delete TEMPLATE_CACHE[templateCacheKey];
|
275 | return callback(e);
|
276 | }
|
277 |
|
278 |
|
279 | executeTemplate(template, templatePath, context, callback);
|
280 | });
|
281 | };
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 | var _init = false;
|
288 | var ensureInit = function(store)
|
289 | {
|
290 | if (_init)
|
291 | {
|
292 | return;
|
293 | }
|
294 |
|
295 | _init = true;
|
296 |
|
297 | if (process.env.CLOUDCMS_APPSERVER_MODE !== "production")
|
298 | {
|
299 |
|
300 | store.watchDirectory("/", function (f, curr, prev) {
|
301 |
|
302 | console.log("Template changes detected - invalidating dust cache");
|
303 | var badKeys = [];
|
304 | for (var k in TEMPLATE_CACHE)
|
305 | {
|
306 | if (k.indexOf(store.id) === 0)
|
307 | {
|
308 | badKeys.push(k);
|
309 | }
|
310 | }
|
311 | for (var i = 0; i < badKeys.length; i++)
|
312 | {
|
313 | console.log("Invalidating watched key: " + badKeys[i]);
|
314 | delete TEMPLATE_CACHE[badKeys[i]];
|
315 | }
|
316 |
|
317 | });
|
318 | }
|
319 | }; |
\ | No newline at end of file |