@ducatus/ducatus-wallet-service-rev
Version:
A service for Mutisig HD Bitcoin Wallets
95 lines (77 loc) • 2.76 kB
text/typescript
import * as fs from 'fs';
import 'source-map-support/register';
import { ExpressApp } from './lib/expressapp';
import { findConfig } from './utils/findConfig';
// const config = require('./config');
const viewConfig = findConfig();
const config = viewConfig && viewConfig !== undefined ? viewConfig : require('./config');
const log = require('npmlog');
log.debug = log.verbose;
log.disableColor();
const port = process.env.BWS_PORT || config.port || 3232;
const cluster = require('cluster');
const serverModule = config.https ? require('https') : require('http');
const serverOpts: {
key?: Buffer;
cert?: Buffer;
ciphers?: string[];
honorCipherOrder?: boolean;
ca?: Buffer[];
} = {};
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 new Error('When running in cluster mode, locker server need to be configured');
if (config.cluster && !config.messageBrokerOpts.messageBrokerServer)
throw new Error('When running in cluster mode, message broker server need to be configured');
const expressApp = new ExpressApp();
function startInstance() {
const server = config.https
? serverModule.createServer(serverOpts, expressApp.app)
: serverModule.Server(expressApp.app);
expressApp.start(config, err => {
console.log(config);
if (err) {
log.error('Could not start BWS instance', err);
return;
}
server.listen(port);
const instanceInfo = cluster.worker ? ' [Instance:' + cluster.worker.id + ']' : '';
log.info('BWS running ' + instanceInfo);
return;
});
}
if (config.cluster && cluster.isMaster) {
// Count the machine's CPUs
const instances = config.clusterInstances || require('os').cpus().length;
log.info('Starting ' + instances + ' instances');
// Create a worker for each CPU
for (let i = 0; i < instances; i += 1) {
cluster.fork();
}
// Listen for dying workers
cluster.on('exit', 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();
}