forked from bitpay/bitcore-wallet-service
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bws.js
executable file
·93 lines (70 loc) · 2.57 KB
/
bws.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
#!/usr/bin/env node
var async = require('async');
var fs = require('fs');
var ExpressApp = require('./lib/expressapp');
var config = require('./config');
var log = require('npmlog');
log.debug = log.verbose;
log.disableColor();
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) {
serverOpts.key = fs.readFileSync(config.privateKeyFile || './ssl/privatekey.pem');
serverOpts.cert = fs.readFileSync(config.certificateFile || './ssl/certificate.pem');
if (config.ciphers) {
serverOpts.ciphers = config.ciphers;
serverOpts.honorCipherOrder = true;
};
// This sets the intermediate CA certs only if they have all been designated in the config.js
if (config.CAinter1 && config.CAinter2 && config.CAroot) {
serverOpts.ca = [fs.readFileSync(config.CAinter1),
fs.readFileSync(config.CAinter2),
fs.readFileSync(config.CAroot)
];
};
}
if (config.cluster && !config.lockOpts.lockerServer)
throw 'When running in cluster mode, locker server need to be configured';
if (config.cluster && !config.messageBrokerOpts.messageBrokerServer)
throw 'When running in cluster mode, message broker server need to be configured';
var expressApp = new ExpressApp();
function startInstance(cb) {
var server = config.https ? serverModule.createServer(serverOpts, expressApp.app) : serverModule.Server(expressApp.app);
server.on('connection', function(socket) {
socket.setTimeout(300 * 1000);
})
expressApp.start(config, function(err) {
if (err) {
log.error('Could not start BWS instance', err);
return;
}
server.listen(port);
var instanceInfo = cluster.worker ? ' [Instance:' + cluster.worker.id + ']' : '';
log.info('BWS running ' + instanceInfo);
return;
});
};
if (config.cluster && cluster.isMaster) {
// Count the machine's CPUs
var instances = config.clusterInstances || require('os').cpus().length;
log.info('Starting ' + instances + ' instances');
// Create a worker for each CPU
for (var i = 0; i < instances; i += 1) {
cluster.fork();
}
// Listen for dying workers
cluster.on('exit', function(worker) {
// Replace the dead worker,
log.error('Worker ' + worker.id + ' died :(');
cluster.fork();
});
// Code to run if we're in a worker process
} else {
log.info('Listening on port: ' + port);
startInstance();
};