async initialize

This commit is contained in:
Matias Alejo Garcia 2015-04-21 14:43:35 -03:00
parent d7ea3e48bb
commit b84c1dc178
5 changed files with 78 additions and 42 deletions

35
bws.js
View File

@ -28,30 +28,35 @@ if (config.https) {
serverOpts.cert = fs.readFileSync(config.certificateFile || './ssl/certificate.pem');
}
var start = function() {
var start = function(cb) {
var server;
if (config.cluster) {
server = sticky(clusterInstances, function() {
var app = ExpressApp.start(config);
var server = config.https ? serverModule.createServer(serverOpts, app) :
ExpressApp.start(config, function(err, app) {
var server = config.https ? serverModule.createServer(serverOpts, app) :
serverModule.Server(app);
WsApp.start(server, config);
return server;
});
});
return cb(server);
} else {
ExpressApp.start(config, function(err, app) {
server = config.https ? serverModule.createServer(serverOpts, app) :
serverModule.Server(app);
WsApp.start(server, config);
return server;
return cb(server);
});
} else {
var app = ExpressApp.start(config);
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);
log.info('Bitcore Wallet Service running on port ' + port);
});
};
};
if (config.cluster && !config.lockOpts.lockerServer)
throw 'When running in cluster mode, locker server need to be configured';
start();
start(function(server) {
server.listen(port, function(err) {
if (err) console.log('ERROR: ', err);
log.info('Bitcore Wallet Service running on port ' + port);
});
});

View File

@ -22,11 +22,11 @@ var ExpressApp = function() {};
* @param opts.WalletService options for WalletService class
* @param opts.basePath
* @param opts.disableLogs
* @param {Callback} cb
*/
ExpressApp.start = function(opts) {
ExpressApp.start = function(opts, cb) {
opts = opts || {};
WalletService.initialize(opts);
var app = express();
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
@ -316,7 +316,10 @@ ExpressApp.start = function(opts) {
app.use(opts.basePath || '/bws/api', router);
return app;
WalletService.initialize(opts, function(err) {
return cb(err,app);
});
};
module.exports = ExpressApp;

View File

@ -53,15 +53,44 @@ WalletService.onNotification = function(func) {
* @param {Object} opts
* @param {Storage} [opts.storage] - The storage provider.
* @param {Storage} [opts.blockchainExplorer] - The blockchainExporer provider.
* @param {Callback} cb
*/
WalletService.initialize = function(opts) {
WalletService.initialize = function(opts, cb) {
$.shouldBeFunction(cb);
opts = opts || {};
lock = opts.lock || new Lock(opts.lockOpts);
// TODO: This method needs to be async
storage = opts.storage || new Storage().connect(opts.storageOpts, function() {});
blockchainExplorer = opts.blockchainExplorer;
blockchainExplorerOpts = opts.blockchainExplorerOpts;
initialized = true;
if (initialized)
return cb();
if (opts.storage) {
storage = opts.storage;
initialized = true;
return cb();
} else {
var newStorage = new Storage();
newStorage.connect(opts.storageOpts, function(err) {
if (err) return cb(err);
storage = newStorage;
initialized = true;
return cb();
});
}
};
WalletService.shutDown = function(cb) {
if (initialized) {
storage.disconnect(function(err) {
if (err) return cb(err);
initialized = false;
return cb();
});
}
};
WalletService.getInstance = function() {

View File

@ -29,7 +29,7 @@ Storage.prototype.connect = function(opts, cb) {
opts = opts || {};
if (this.db) return cb();
if (this.db) return cb(null);
var config = opts.mongoDb || {};
var url = 'mongodb://' + (config.host || 'localhost') + ':' + (config.port ||  27017) + '/bws';
@ -40,6 +40,16 @@ Storage.prototype.connect = function(opts, cb) {
}
self.db = db;
console.log('Connection established to ', url);
return cb(null);
});
};
Storage.prototype.disconnect = function(cb) {
var self = this;
this.db.close(true, function(err) {
if (err) return cb(err);
self.db = null;
return cb();
});
};

View File

@ -227,15 +227,6 @@ function resetDb(cb) {
});
};
function closeDb(cb) {
if (!db) return cb();
db.close(true, function(err) {
should.not.exist(err);
db = null;
return cb();
});
};
describe('Wallet service', function() {
before(function(done) {
@ -249,21 +240,20 @@ describe('Wallet service', function() {
beforeEach(function(done) {
resetDb(function() {
blockchainExplorer = sinon.stub();
WalletService.initialize({
storage: storage,
blockchainExplorer: blockchainExplorer,
}, function() {
helpers.offset = 0;
done();
});
helpers.offset = 0;
done();
});
});
after(function(done) {
closeDb(done);
WalletService.shutDown(done);
});
describe('#getInstanceWithAuth', function() {
beforeEach(function() {});
it('should get server instance for existing copayer', function(done) {
@ -3066,21 +3056,20 @@ describe('Blockchain monitor', function() {
resetDb(function() {
blockchainExplorer = sinon.stub();
WalletService.initialize({
storage: storage,
blockchainExplorer: blockchainExplorer,
}, function() {
helpers.offset = 0;
done();
});
helpers.offset = 0;
done();
});
});
afterEach(function() {
BlockchainMonitor.prototype._getAddressSubscriber.restore();
});
after(function(done) {
closeDb(done);
WalletService.shutDown(done);
});
it('should subscribe wallet', function(done) {