Commit 02f5b8ef authored by priit's avatar priit
Browse files

Mitme lõime toetus

parent 6f4c7c2d
......@@ -9,6 +9,8 @@ var logger = require('morgan');
var bodyParser = require('body-parser');
var multer = require('multer');
var debug = require('debug')('keeleliin-wrapper:server');
var cluster = require('cluster');
var http = require('http');
var config = require('./config');
var controllers = require('./controllers');
......@@ -22,6 +24,8 @@ log4js.configure({
{ type: 'file', filename: 'keeleliin.log' }
]
});
var log4jsLogger = log4js.getLogger('app_js');
app.use(logger('dev'));
app.use(bodyParser.json({limit: '1000mb'})); // for parsing application/json
app.use(multer({ dest: './uploads/'})); // for parsing multipart/form-data
......@@ -34,29 +38,39 @@ app.use(errorhandlerMiddleware.error404);
/**
* Create HTTP server.
*/
var port = normalizePort(config.port);
app.set('port', port);
var server = http.createServer(app);
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
var numCPUs = require('os').cpus().length;
log4jsLogger.debug('NumCPUs: ' + numCPUs);
///
if (cluster.isMaster) {
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
log4jsLogger.info('worker ' + worker.process.pid + ' died; Code' + code + '; Signal: ' + signal);
});
} else {
// Workers can share any TCP connection
// In this case its a HTTP server
var port = normalizePort(config.port);
app.set('port', port);
var server = http.createServer(app);
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
}
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
......@@ -93,7 +107,7 @@ function onError(error) {
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port;
debug('Listening on ' + bind);
log4jsLogger.debug('Listening on ' + bind);
}
module.exports = app;
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment