From 3a45a2c41650194899ca8ef807adf0e0a80bbd6c Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Sun, 5 Apr 2015 13:56:56 -0300 Subject: [PATCH] add config.js file --- app.js | 8 +++----- config.js | 24 ++++++++++++++++++++++++ lib/blockchainexplorer.js | 12 +++--------- lib/blockchainmonitor.js | 10 +++++++--- lib/server.js | 2 +- lib/wsapp.js | 5 ++--- 6 files changed, 40 insertions(+), 21 deletions(-) create mode 100644 config.js diff --git a/app.js b/app.js index c13a39e..b55071e 100644 --- a/app.js +++ b/app.js @@ -2,18 +2,16 @@ var ExpressApp = require('./lib/expressapp'); var WsApp = require('./lib/wsapp'); +var config = require('./config'); -var basePath = process.env.BWS_BASE_PATH || '/bws/api'; var port = process.env.BWS_PORT || 3001; -var app = ExpressApp.start({ - basePath: basePath, -}); +var app = ExpressApp.start(config); //app.listen(port); var server = require('http').Server(app); -var ws = WsApp.start(server); +var ws = WsApp.start(server, config); server.listen(port); diff --git a/config.js b/config.js new file mode 100644 index 0000000..d017e77 --- /dev/null +++ b/config.js @@ -0,0 +1,24 @@ +var config = { + basePath: '/bws/api', + disableLogs: false, + BlockchainMonitor: { + livenet: { + name: 'insight', + url: 'https://insight.bitpay.com:443', + }, + testnet: { + name: 'insight', + url: 'https://test-insight.bitpay.com:443', + }, + }, + WalletService: { + storageOpts: { + dbPath: './db', + /* + dbHost: 'http://db.host.org', + dbPort: '8188', + */ + }, + }, +}; +module.exports = config; diff --git a/lib/blockchainexplorer.js b/lib/blockchainexplorer.js index 388f3c4..e66f670 100644 --- a/lib/blockchainexplorer.js +++ b/lib/blockchainexplorer.js @@ -14,19 +14,13 @@ function BlockChainExplorer(opts) { $.checkArgument(opts); var provider = opts.provider || 'insight'; var network = opts.network || 'livenet'; + var dfltUrl = network == 'livenet' ? 'https://insight.bitpay.com:443' : + 'https://test-insight.bitpay.com:443'; + var url = opts.url || dfltUrl; var url; switch (provider) { case 'insight': - switch (network) { - default: - case 'livenet': - url = 'https://insight.bitpay.com:443'; - break; - case 'testnet': - url = 'https://test-insight.bitpay.com:443' - break; - } var explorer = new Explorers.Insight(url, network); explorer.getTransactions = _.bind(getTransactionsInsight, explorer, url); explorer.getAddressActivity = _.bind(getAddressActivityInsight, explorer, url); diff --git a/lib/blockchainmonitor.js b/lib/blockchainmonitor.js index 2947e72..4135a05 100644 --- a/lib/blockchainmonitor.js +++ b/lib/blockchainmonitor.js @@ -17,12 +17,16 @@ var BlockchainExplorer = require('./blockchainexplorer'); var Notification = require('./model/notification'); -function BlockchainMonitor() { +function BlockchainMonitor(opts) { + opts = opts || {}; var self = this; this.subscriptions = {}; this.subscriber = {}; - this.subscriber['livenet'] = self._getAddressSubscriber('insight', 'livenet'); - this.subscriber['testnet'] = self._getAddressSubscriber('insight', 'testnet'); + _.each(['livenet', 'testnet'], function(network) { + opts[network] = opts[network] || {}; + self.subscriber[network] = self._getAddressSubscriber( + opts[network].name, network, opts[network].url); + }); }; nodeutil.inherits(BlockchainMonitor, events.EventEmitter); diff --git a/lib/server.js b/lib/server.js index 23cad98..d181d4b 100644 --- a/lib/server.js +++ b/lib/server.js @@ -52,7 +52,7 @@ WalletService.onNotification = function(func) { */ WalletService.initialize = function(opts) { opts = opts || {}; - storage = opts.storage ||  new Storage(); + storage = opts.storage ||  new Storage(opts.storageOpts); blockchainExplorer = opts.blockchainExplorer; initialized = true; }; diff --git a/lib/wsapp.js b/lib/wsapp.js index 2a56a38..200bbf3 100644 --- a/lib/wsapp.js +++ b/lib/wsapp.js @@ -32,10 +32,9 @@ WsApp.handleNotification = function(service, notification) { io.to(notification.walletId).emit('notification', notification); }; -WsApp.start = function(server) { +WsApp.start = function(server, config) { io = require('socket.io')(server); - - bcMonitor = new BlockchainMonitor(); + bcMonitor = new BlockchainMonitor(config.BlockchainMonitor); function handleNotification(notification) { if (notification.type == 'NewAddress') {