Browse Source

Merge pull request #204 from isocolsky/ref/launcher

Refactor BWS launch script
activeAddress
Matias Alejo Garcia 10 years ago
parent
commit
ebc779fb02
  1. 48
      bws.js
  2. 23
      lib/expressapp.js

48
bws.js

@ -1,5 +1,6 @@
#!/usr/bin/env node #!/usr/bin/env node
var async = require('async');
var fs = require('fs'); var fs = require('fs');
var ExpressApp = require('./lib/expressapp'); var ExpressApp = require('./lib/expressapp');
@ -29,32 +30,39 @@ if (config.https) {
} }
var start = function(cb) { 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) { if (config.cluster) {
server = sticky(clusterInstances, function() { var server = sticky(clusterInstances, function() {
ExpressApp.start(config, function(err, app) { return doStart();
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;
});
});
}); });
return cb(null, server); return cb(null, server);
} else { } else {
ExpressApp.start(config, function(err, app) { var server = doStart(function(err) {
if (err) return cb(err); return cb(err, server);
server = config.https ? serverModule.createServer(serverOpts, app) :
serverModule.Server(app);
var wsApp = new WsApp();
wsApp.start(server, config, function(err) {
return cb(err, server);
});
}); });
}; }
}; };
if (config.cluster && !config.lockOpts.lockerServer) if (config.cluster && !config.lockOpts.lockerServer)

23
lib/expressapp.js

@ -14,7 +14,9 @@ log.disableColor();
log.debug = log.verbose; log.debug = log.verbose;
log.level = 'debug'; log.level = 'debug';
var ExpressApp = function() {}; var ExpressApp = function() {
this.app = express();
};
/** /**
* start * start
@ -24,11 +26,10 @@ var ExpressApp = function() {};
* @param opts.disableLogs * @param opts.disableLogs
* @param {Callback} cb * @param {Callback} cb
*/ */
ExpressApp.start = function(opts, cb) { ExpressApp.prototype.start = function(opts, cb) {
opts = opts || {}; opts = opts || {};
var app = express(); this.app.use(function(req, res, next) {
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE'); 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'); 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(); next();
} }
app.use(allowCORS); this.app.use(allowCORS);
app.enable('trust proxy'); this.app.enable('trust proxy');
var POST_LIMIT = 1024 * 100 /* Max POST 100 kb */ ; var POST_LIMIT = 1024 * 100 /* Max POST 100 kb */ ;
app.use(bodyParser.json({ this.app.use(bodyParser.json({
limit: POST_LIMIT limit: POST_LIMIT
})); }));
@ -58,7 +59,7 @@ ExpressApp.start = function(opts, cb) {
//var accessLogStream = fs.createWriteStream(__dirname + '/access.log', {flags: 'a'}) //var accessLogStream = fs.createWriteStream(__dirname + '/access.log', {flags: 'a'})
//app.use(morgan('combined', {stream: accessLogStream})) //app.use(morgan('combined', {stream: accessLogStream}))
// app.use(require('morgan')('dev')); // 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) { WalletService.initialize(opts, cb);
return cb(err,app);
});
}; };
module.exports = ExpressApp; module.exports = ExpressApp;

Loading…
Cancel
Save