-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent.js
More file actions
222 lines (201 loc) · 7.55 KB
/
content.js
File metadata and controls
222 lines (201 loc) · 7.55 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
const babelify = require('babelify');
const browserify = require('browserify');
const config = require('@cheevr/config');
const express = require('express');
const fs = require('fs');
const lang = require('@cheevr/lang');
const Logger = require('@cheevr/logging');
const path = require('path');
const Router = require('versioned-api-router');
const stylus = require('stylus');
const uglify = require('uglify-js');
const util = require('util');
const cwd = process.cwd();
const hostname = require('os').hostname();
const application = path.basename(path.dirname(require.main.filename));
const viewDir = config.normalizePath(cwd, config.paths.views);
const stylesDir = config.normalizePath(cwd, config.paths.styles);
const imgDir = config.normalizePath(cwd, config.paths.img);
const jsDir = config.normalizePath(cwd, config.paths.js);
const serverJsDir = path.join(__dirname, 'static/js');
const jsScanInterval = 1000;
const cacheDir = path.isAbsolute(config.paths.cache) ? config.paths.cache : path.join(cwd, config.paths.cache);
const existingViews = {};
const jsProcessed = {};
viewDir.push(path.join(__dirname, 'static/views'));
stylesDir.push(path.join(__dirname, 'static/styles'));
imgDir.push(path.join(__dirname, 'static/img'));
module.exports = app => {
// TODO if coming from a mobile browser try to load file.mobile.ext first and then fall back to file.ext.
// Convert stylus files to css and place them in cache dir
for (let dir of stylesDir) {
fs.existsSync(dir) && app.use('/css', stylus.middleware({
src: dir,
dest: cacheDir,
compress: config.isProd,
sourcemap: !config.isProd
}));
}
// Process all javascript files with browserify and babel
app.use('/js', processJs());
config.isProd || [...jsDir, serverJsDir].forEach(dir => {
fs.existsSync(dir) && fs.watch(dir, {interval: jsScanInterval, recursive: true}, () => {
Object.keys(jsProcessed).forEach(prop => delete jsProcessed[prop]);
clearRecursive(cacheDir, '.js');
});
});
// Serve static files
let staticConfig = config.isProd ? { maxAge: config.backend.cacheTime } : {};
fs.existsSync(cacheDir) && app.use('/css|/js', express.static(cacheDir, staticConfig));
for (let dir of imgDir) {
fs.existsSync(dir) && app.use('/img', express.static(dir, staticConfig));
}
// Replace rendering function to look in multitple locations
app.get(/.*/, (req, res, next) => {
let original = res.render;
res.render = (file, dict = lang.dictionary) => {
file = String(file);
file = file.length ? file : 'index';
for (let dir of viewDir) {
let fullPath = path.join(dir, file + '.pug');
if (existingViews[fullPath] === undefined) {
existingViews[fullPath] = fs.existsSync(fullPath);
}
if (existingViews[fullPath]) {
original.call(res, fullPath, {
basedir: dir,
dict,
user: req.user
});
res.done = true;
return res;
}
}
return res;
};
next();
});
// Include routes from the projects route directory
let routePaths = Array.isArray() ? config.paths.routes : [ config.paths.routes ];
let router = new Router();
router.auth = app.auth;
for (let routePath of routePaths) {
let dir = path.isAbsolute(routePath) ? routePath : path.join(cwd, routePath);
if (fs.existsSync(dir)) {
let files = fs.readdirSync(dir);
for (let file of files) {
require(path.join(dir, file))(router);
}
}
}
app.use(router);
// Automatically serve view files if they exist.
app.get('*', (req, res, next) => {
let file = req.path.replace(/^\//, '').replace(/\.(pug|html?|php|asp|jsp|py|rb|xml)$/, '');
res.render(file);
res.done || next();
});
// Feedback handler
feedback(app);
// File Not Found handler
app.use((req, res) => {
res.status(404);
let acceptType = req.headers['accept'] || '';
if (req.method == 'GET' && acceptType.indexOf('html') !== -1) {
return res.render(404);
}
res.end();
});
// Error handler
app.use(function (error, req, res, next) {
Logger.server.error(util.format(error));
res.statusCode > 399 || res.status(500);
let acceptType = req.headers['accept'] || '';
if (req.method == 'GET' && acceptType.indexOf('html') !== 0) {
return res.render(500);
}
res.end();
});
};
function clearRecursive(dir, filter) {
fs.readdir(dir, (err, files) => {
for (let file of files) {
let fullPath = path.join(dir, file);
if (fs.existsSync(fullPath)) {
let stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
clearRecursive(fullPath, filter);
} else {
path.extname(file) == filter && fs.unlinkSync(fullPath);
}
}
}
})
}
function processJs() {
return (req, res, next) => {
let file = req.originalUrl.replace(/^\/js\//, '');
let fileProps = path.parse(file);
req.url = req.url.replace(fileProps.ext, '.' + req.locale + fileProps.ext);
if (jsProcessed[file]) {
return next('route');
}
let targetFile = path.join(cacheDir, fileProps.dir, fileProps.name + '.' + req.locale + fileProps.ext);
fs.existsSync(targetFile) && fs.unlinkSync(targetFile);
let targetDir = path.dirname(targetFile);
fs.existsSync(targetDir) || fs.mkdirSync(targetDir);
let returned = false;
for (let dir of jsDir) {
let sourceFile = path.join(dir, file);
if (!fs.existsSync(sourceFile)) {
continue;
}
browserify(sourceFile, {
paths: serverJsDir,
basedir: dir,
debug: !config.isProd
}).transform(babelify, {
presets: ['es2015']
}).bundle((err, content) => {
if (err) {
return console.log('Error compiling javascript', err);
}
content = lang.process(content.toString('utf8'), req.locale);
if (config.isProd) {
content = uglify.minify(content, {fromString: true}).code;
}
fs.writeFileSync(targetFile, content, 'utf8');
jsProcessed[file] = true;
if (!returned) {
returned = true;
next('route');
}
});
}
}
}
function feedback(app) {
app.post('/feedback', app.noauth, (req, res) => {
let feedback = req.body;
if (!feedback.message || !feedback.message.length) {
return res.status(422).end();
}
req.user && (feedback.userid = req.user.id);
feedback.ip = req.ip;
feedback['@timestamp'] = new Date();
feedback.tier = config.tier;
feedback.process = process.pid;
feedback.hostname = hostname;
feedback.application = application;
req.es.index({
index: 'feedback',
type: 'entry',
body: feedback
}, err => {
if (err) {
throw err;
}
res.end();
});
});
}