|
|
@ -13,8 +13,9 @@ var port = process.env.BWS_PORT || config.port || 3232; |
|
|
|
var cluster = require('cluster'); |
|
|
|
var http = require('http'); |
|
|
|
var numCPUs = require('os').cpus().length; |
|
|
|
|
|
|
|
var clusterInstances = config.clusterInstances || numCPUs; |
|
|
|
var serverModule = config.https ? require('https') : require('http'); |
|
|
|
|
|
|
|
var serverOpts = {}; |
|
|
|
|
|
|
|
if (config.https) { |
|
|
@ -22,31 +23,29 @@ if (config.https) { |
|
|
|
serverOpts.cert = fs.readFileSync(config.certificateFile || './ssl/certificate.pem'); |
|
|
|
} |
|
|
|
|
|
|
|
var startOne = function() { |
|
|
|
var start = function() { |
|
|
|
var app = ExpressApp.start(config); |
|
|
|
var server = config.https ? sticky(serverModule.createServer(serverOpts, app)) : |
|
|
|
serverModule.Server(app); |
|
|
|
var ws = WsApp.start(server, config); |
|
|
|
server.listen(port); |
|
|
|
console.log('Bitcore Wallet Service running on port ' + port); |
|
|
|
}; |
|
|
|
|
|
|
|
if (!config.cluster) { |
|
|
|
startOne(); |
|
|
|
} else { |
|
|
|
if (!config.storageOpts.multiLevel || !config.lockOpts.lockerServer) |
|
|
|
throw 'When running in cluster mode, multilevel and locker server need to be configured'; |
|
|
|
|
|
|
|
var clusterInstances = config.clusterInstances || numCpus; |
|
|
|
|
|
|
|
if (cluster.isMaster) { |
|
|
|
for (var i = 0; i < clusterInstances; i++) { |
|
|
|
cluster.fork(); |
|
|
|
} |
|
|
|
cluster.on('exit', function(worker, code, signal) { |
|
|
|
console.log('worker ' + worker.process.pid + ' died'); |
|
|
|
var server; |
|
|
|
|
|
|
|
if (config.cluster) { |
|
|
|
server = sticky(clusterInstances, function() { |
|
|
|
var server = config.https ? serverModule.createServer(serverOpts, app) : |
|
|
|
serverModule.Server(app); |
|
|
|
WsApp.start(server, config); |
|
|
|
return server; |
|
|
|
}); |
|
|
|
} else { |
|
|
|
startOne(); |
|
|
|
server = config.https ? serverModule.createServer(serverOpts, app) : |
|
|
|
serverModule.Server(app); |
|
|
|
WsApp.start(server, config); |
|
|
|
} |
|
|
|
} |
|
|
|
server.listen(port, function(err) { |
|
|
|
if (err) console.log('ERROR: ', err); |
|
|
|
console.log('Bitcore Wallet Service running on port ' + port); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
if (config.cluster && (!config.storageOpts.multiLevel || !config.lockOpts.lockerServer)) |
|
|
|
throw 'When running in cluster mode, multilevel and locker server need to be configured'; |
|
|
|
|
|
|
|
start(); |
|
|
|