-
Notifications
You must be signed in to change notification settings - Fork 24
/
server.js
76 lines (66 loc) · 2.05 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
const express = require('express')
, cluster = require('cluster')
, numCpus = require('os').cpus().length
, bodyParser = require('body-parser')
, fs = require('fs')
, http = require('http')
, mongoose = require('mongoose')
, morgan = require('morgan')
, compress = require('compression')
, logger = require('logfmt')
, routes = require('./routes')
, reports = require('./routes/reports')
, locations = require('./routes/locations')
, fauxAuth = require('./middleware/staging-auth')
, concurrency = process.env.WEB_CONCURRENCY
;
const app = express()
, env = app.get('env')
, config = require('./config/config')[env]
, dbCnx = process.env.MONGOLAB_URI || config.db
, db = mongoose.connect(dbCnx)
, port = process.env.PORT || config.port || 3000
;
// memjs reads appropriate env variables by default.
// zero configuration necessary
app.set('view engine','ejs');
app.engine('html', require('ejs').renderFile);
app.use(compress());
app.use(express.static('public'));
app.set('port', port)
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
switch (app.get('env')) {
case 'development':
app.use(morgan('dev'));
break;
case 'staging':
app.use(fauxAuth);
break;
default:
app.use(morgan('combined'));
break;
}
app.use(require('./routes/index.js'));
app.get('/reports/count.json', reports.count);
app.get('/reports/:id.json', reports.show);
app.get('/reports.json', reports.index);
app.post('/reports.json', reports.create);
app.get('/locations.json', locations.index);
app.get('/locations/count.json', locations.count);
app.use(require('./routes/wards.js'));
require('./routes/admin.js')(app);
if (env != 'development' && cluster.isMaster) {
for (var i = 0; i < concurrency; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
logger.log({worker: worker.process.pid, msg: 'died', code: code, signal: signal});
});
} else {
http.createServer(app).listen(port, function() {
logger.log({status: 'info', msg: 'server listening', port: port});
});
}