forked from npr/potluck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
128 lines (110 loc) · 4.15 KB
/
server.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Define third-party libraries
var util = require('util')
, cluster = require('cluster')
, express = require('express')
, thumbs = require('connect-thumbs')
, app = express()
, _ = require('underscore')
, CONF = require('config')
, less = require('less')
, log = require('tracer').console()
, hbs = require('hbs');
// Potluck libraries
var asset = require('./lib/asset');
//-- Useful extension to HBS
require('./lib/handlebars-helpers.js')(hbs, __dirname + '/views/partials');
var pub_dir = CONF.app.pub_dir;
if (pub_dir[0] != '/') { pub_dir = '/' + pub_dir; } // humans are forgetful
pub_dir = __dirname + pub_dir;
/**
* All environments
*/
app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'handlebars');
app.engine('handlebars', hbs.__express);
app.use(thumbs({
"decodeFn": asset.lookup
, "ttl": 3600*24*30
, "tmpCacheTTL": 3600*24*90
}));
/**
app.use(thumbs({
rootURL: "/images"
, tmpPath: "/tmp"
, cache: true
}));**/
app.use(express.bodyParser({ keepExtensions: true, uploadDir: '/tmp/files' }));
app.use(express.methodOverride());
app.use(express.query());
app.use(express.cookieParser(CONF.app.cookie_secret));
app.use(express.session({secret: CONF.app.cookie_secret}));
//app.use(express.cookieSession({secret: CONF.app.cookie_secret}));
app.use(app.router);
//app.use(express.responseTime());
// This is not needed if you handle static files with, say, Nginx (recommended in production!)
// Additionally you should probably precompile your LESS stylesheets in production
// Last, but not least: Express' default error handler is very useful in dev, but probably not in prod.
if ((typeof process.env['NODE_SERVE_STATIC'] !== 'undefined') && process.env['NODE_SERVE_STATIC'] == 1) {
app.use(require('less-middleware')({ src: pub_dir+"/css/less", dest: pub_dir + "/css", force:true}));
app.use(express.static(pub_dir));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
}
// Catch-all error handler. Override as you see fit
app.use(function(err, req, res, next){
console.error(err.stack);
res.send(500, 'An unexpected error occurred! Please check logs.');
});
});
//-- Routes configuration is externalized in a different module (can be multiple) for cleaner code.
module.exports.app = app;
require('./lib/routes');
//-- End routes configuration.
var numCPUs = require('os').cpus().length;
if (cluster.isMaster
&& (process.env.NODE_CLUSTERED === 1)) {
util.log("Starting app in clustered mode");
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('fork', function(worker) {
util.log('Forking worker #', worker.id);
timeouts[worker.id] = setTimeout(function() {
util.error(['Worker taking too long to start']);
}, 2000);
});
cluster.on('listening', function(worker, address) {
util.log('Worker #'+worker.id+' listening on port: ' + address.port);
clearTimeout(timeouts[worker.id]);
});
cluster.on('online', function(worker) {
util.log('Worker #'+worker.id+' is online');
});
cluster.on('exit', function(worker, code, signal) {
util.error(['The worker #'+worker.id+' has exited with exitCode ' + worker.process.exitCode]);
clearTimeout(timeouts[worker.id]);
// Don't try to restart the workers when disconnect or destroy has been called
if(worker.suicide !== true) {
util.debug('Worker #' + worker.id + ' did not commit suicide, restarting');
cluster.fork();
}
});
cluster.on('disconnect', function(worker) {
util.debug('The worker #' + worker.id + ' has disconnected');
});
// Trick suggested by Ian Young (https://github.com/isaacs/node-supervisor/issues/40#issuecomment-4330946)
// to make cluster and supervisor play nicely together:
if (process.env.NODE_HOT_RELOAD === 1) {
var signals = ['SIGINT', 'SIGTERM', 'SIGQUIT'];
_.each(signals, function(signal){
process.on(signal, function(){
_.each(cluster.workers, function(worker){
worker.destroy();
})
})
})
}
} else {
app.listen(CONF.app.port);
util.log("Express server instance listening on port " + CONF.app.port);
}