Cache templates

This commit is contained in:
Thomas Forgione 2017-09-30 11:28:26 +02:00
parent b600c585bc
commit 38bc43e3e7
No known key found for this signature in database
GPG Key ID: 3B8FC64F5BBFE109
2 changed files with 34 additions and 3 deletions

View File

@ -40,10 +40,16 @@ function startServer() {
log.request(req, res, Date.now() - start); log.request(req, res, Date.now() - start);
}); });
res.locals.session = req.session; res.locals.session = req.session;
// if (req.session.user !== undefined) {
// req.session.user = User.fromJSON(req.session.user);
// }
req.session.user = {
username: "thomas",
_username: {
value: "thomas",
},
};
if (req.session.user !== undefined) {
req.session.user = User.fromJSON(req.session.user);
}
next(); next();
}); });

View File

@ -1,10 +1,18 @@
const express = require('express'); const express = require('express');
const fs = require('fs'); const fs = require('fs');
const log = require('log'); const log = require('log');
const pug = require('pug');
const config = require('settings/config');
module.exports = function(app, controllersDir = __dirname + '/../controllers') { module.exports = function(app, controllersDir = __dirname + '/../controllers') {
log.debug("Loading controllers :"); log.debug("Loading controllers :");
let templatesToCompile = [];
let templatesDir = controllersDir + '/../templates';
fs.readdirSync(templatesDir).forEach((templ) => {
templatesToCompile.push(templatesDir + '/' + templ);
});
fs.readdirSync(controllersDir).forEach(function(name) { fs.readdirSync(controllersDir).forEach(function(name) {
@ -15,6 +23,11 @@ module.exports = function(app, controllersDir = __dirname + '/../controllers') {
let urls = require(controllersDir + '/' + name + '/urls'); let urls = require(controllersDir + '/' + name + '/urls');
name = obj.name || name; name = obj.name || name;
let templatesDir = controllersDir + '/' + name + '/templates';
fs.readdirSync(templatesDir).forEach((templ) => {
templatesToCompile.push(templatesDir + '/' + templ);
});
// allow specifying the view engine // allow specifying the view engine
if (obj.engine) app.set('view engine', obj.engine); if (obj.engine) app.set('view engine', obj.engine);
@ -43,9 +56,21 @@ module.exports = function(app, controllersDir = __dirname + '/../controllers') {
log.debug(); log.debug();
// mount the app // mount the app
// parent.use(app); // parent.use(app);
}); });
if (config.DEBUG === false) {
let time = Date.now();
log.debug('Compiling templates...');
for (let template of templatesToCompile) {
if (template.substr(-4) !== '.txt') {
pug.compileFile(template, {cache: true});
}
}
log.debug("Templates compiled in " + (Date.now() - time) + 'ms');
}
} }