-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
109 lines (105 loc) · 3.67 KB
/
index.js
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
var express = require('express'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
logger = require('morgan'),
compression = require('compression'),
helmet = require('helmet'),
path = require('path'),
debug = require('debug')('saucier:main'),
processData = require('./lib/processData'),
renderHandler = require('./lib/renderHandler'),
cluster = require('cluster')
app = express(),
webWorkers = {};
/**
* Saucier
* @param {object} saucierGet Handles a series of tasks
* related to fetching items
* from an external API.
* @param {object} saucierCache Handles a series of tasks
* related to caching items.
* @param {object} templateEngine The template engine.
* @param {object} config Saucier configuration
* @return {object} The Saucier app.
*/
module.exports = function saucier(saucierGet, saucierCache, templateEngine, config){
var handleGet = function (route, template, options) {
/**
* Handle GET responses in a generic way
* @private
* @param {(string|regexp)} route The route to match.
* @param {string} template Template name that will be used to format the response
* @param {object} options An object that configures the Controller instance
* @return {object} res The response object
*/
app.get(
route,
function (req, res, next) {
req.saucier = req.saucier || {};
req.saucier.routeOptions = options;
req.saucier.applicationConfig = config;
req.saucier.environment = process.env.NODE_ENV || 'local';
next();
},
saucierCache.get,
saucierGet.get(saucierGet.httpAgent),
saucierCache.set(900),
processData.prepare,
renderHandler(templateEngine, template),
function (req, res) {
return res.send(req.saucier.response);
}
);
},
spawnWebWorker = function () {
var worker = cluster.fork({type: 'webWorker'});
webWorkers[worker.process.pid] = worker;
debug('Starting web worker with pid: %d.', worker.process.pid);
return worker;
},
handleStart = function () {
/**
* Start the server
* @alias start
*/
var workers = process.env.WEB_CONCURRENCY || 1,
port = process.env.PORT || 3000,
x;
if (cluster.isMaster) {
for (x = 0; x < workers; x++) {
spawnWebWorker();
}
cluster.on('exit', function (worker, code) {
if (code === 0) {
return;
}
debug('Worker %d died. Spawning a new process', worker.process.pid);
if (webWorkers[worker.process.pid]) {
webWorkers[worker.process.pid] = null;
delete webWorkers[worker.process.pid];
spawnWebWorker();
}
});
}
else {
return app.listen(3000, function headlessdrupalListen() {
debug('ENVIRONMENT: %s', 'local');
debug('SAUCIER is running on port %s.', 3000);
});
}
};
app
.use(compression())
.use(logger(config.logFormat || 'dev'))
.use(bodyParser.json())
.use(bodyParser.urlencoded({extended: false}))
.use(cookieParser())
.use(helmet())
.use(express.static(path.join(__dirname, '..', config.staticDir || 'public'), {
maxAge: config.maxAge || '4d'
}))
.use(saucierCache.create())
app.handleGet = handleGet;
app.start = handleStart;
return app;
}