diff --git a/bws.js b/bws.js index 7812e29..b908fec 100755 --- a/bws.js +++ b/bws.js @@ -1,5 +1,6 @@ #!/usr/bin/env node +var async = require('async'); var fs = require('fs'); var ExpressApp = require('./lib/expressapp'); @@ -29,32 +30,39 @@ if (config.https) { } var start = function(cb) { - var server; + var expressApp = new ExpressApp(); + var wsApp = new WsApp(); + + function doStart(cb) { + var server = config.https ? serverModule.createServer(serverOpts, expressApp.app) : serverModule.Server(expressApp.app); + async.parallel([ + + function(done) { + expressApp.start(config, done); + }, + function(done) { + wsApp.start(server, config, done); + }, + ], function(err) { + if (err) { + log.error('Could not start BWS instance', err); + } + if (cb) return cb(err); + }); + + return server; + }; if (config.cluster) { - server = sticky(clusterInstances, function() { - ExpressApp.start(config, function(err, app) { - if (err) return cb(err); - var server = config.https ? serverModule.createServer(serverOpts, app) : - serverModule.Server(app); - var wsApp = new WsApp(); - wsApp.start(server, config, function(err) { - return server; - }); - }); + var server = sticky(clusterInstances, function() { + return doStart(); }); return cb(null, server); } else { - ExpressApp.start(config, function(err, app) { - if (err) return cb(err); - server = config.https ? serverModule.createServer(serverOpts, app) : - serverModule.Server(app); - var wsApp = new WsApp(); - wsApp.start(server, config, function(err) { - return cb(err, server); - }); + var server = doStart(function(err) { + return cb(err, server); }); - }; + } }; if (config.cluster && !config.lockOpts.lockerServer) diff --git a/lib/expressapp.js b/lib/expressapp.js index 318387e..4b7dad2 100644 --- a/lib/expressapp.js +++ b/lib/expressapp.js @@ -14,7 +14,9 @@ log.disableColor(); log.debug = log.verbose; log.level = 'debug'; -var ExpressApp = function() {}; +var ExpressApp = function() { + this.app = express(); +}; /** * start @@ -24,11 +26,10 @@ var ExpressApp = function() {}; * @param opts.disableLogs * @param {Callback} cb */ -ExpressApp.start = function(opts, cb) { +ExpressApp.prototype.start = function(opts, cb) { opts = opts || {}; - var app = express(); - app.use(function(req, res, next) { + this.app.use(function(req, res, next) { res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE'); res.setHeader('Access-Control-Allow-Headers', 'x-signature,x-identity,X-Requested-With,Content-Type,Authorization'); @@ -42,12 +43,12 @@ ExpressApp.start = function(opts, cb) { } next(); } - app.use(allowCORS); - app.enable('trust proxy'); + this.app.use(allowCORS); + this.app.enable('trust proxy'); var POST_LIMIT = 1024 * 100 /* Max POST 100 kb */ ; - app.use(bodyParser.json({ + this.app.use(bodyParser.json({ limit: POST_LIMIT })); @@ -58,7 +59,7 @@ ExpressApp.start = function(opts, cb) { //var accessLogStream = fs.createWriteStream(__dirname + '/access.log', {flags: 'a'}) //app.use(morgan('combined', {stream: accessLogStream})) // app.use(require('morgan')('dev')); - app.use(require('morgan')(':remote-addr :date[iso] ":method :url" :status :res[content-length] :response-time ":user-agent" ')); + this.app.use(require('morgan')(':remote-addr :date[iso] ":method :url" :status :res[content-length] :response-time ":user-agent" ')); } @@ -315,11 +316,9 @@ ExpressApp.start = function(opts, cb) { }); - app.use(opts.basePath || '/bws/api', router); + this.app.use(opts.basePath || '/bws/api', router); - WalletService.initialize(opts, function(err) { - return cb(err,app); - }); + WalletService.initialize(opts, cb); }; module.exports = ExpressApp;