-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
56 lines (51 loc) · 1.72 KB
/
app.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
var bodyParser = require('body-parser')
, cluster = require('cluster')
, express = require('express')
, expressJson = require('express-json')
, expressWinston = require('express-winston')
, swagger_node = require('swagger-node-express')
, swaggerResources = require('./swaggerResources.js')
, url = require('url')
, winston = require('winston')
, _ = require('lodash');
if (cluster.isMaster) {
var nCpus = require('os').cpus().length;
console.log('Initializing app');
for (var i=0; i<_.min([4, nCpus]); i++) {
cluster.fork();
}
cluster.on('exit', function(worker) {
console.log('Worker %d died; replacing', worker.id);
cluster.fork();
});
} else {
startApp();
}
function startApp() {
var app = express();
app.use(expressJson());
//app.use(bodyParser.urlencoded());
app.use('/swagger', express.static(__dirname + '/public/swagger'));
app.use(expressWinston.logger({
transports: [
new winston.transports.Console({
colorize: true
})
//transports can be custom functions, built in, or user supported:
//file, http, mongoDB, couchDB, mysql, loggly, redis, mail
]
}));
var swagger = swagger_node.createNew(app);
swaggerResources.addRoutes(swagger);
startServer(app, swagger);
}
function startServer(app, swagger) {
swagger.configureSwaggerPaths("", "/docs", "");
swagger.configure('http://localhost:3000/','0.1');
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Worker %d listening at http://%s:%s',
cluster.worker.id, host, port);
});
}