From 99e2ac2051a5f232ec52cbe0992c2f759d9c4db0 Mon Sep 17 00:00:00 2001 From: Nadav Ivgi Date: Tue, 5 Feb 2019 14:37:49 +0200 Subject: [PATCH] Accept bitcoind rpc and influxdb options as a connection URI --- app/defaultCredentials.js | 30 +++++++++++++++++++----------- bin/cli.js | 17 +++++++++++++---- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/app/defaultCredentials.js b/app/defaultCredentials.js index 9218742..664dacd 100644 --- a/app/defaultCredentials.js +++ b/app/defaultCredentials.js @@ -1,22 +1,30 @@ var os = require('os'); var path = require('path'); +var url = require('url'); + +var btcUri = process.env.BTCEXP_BITCOIND_URI ? url.parse(process.env.BTCEXP_BITCOIND_URI, true) : { query: { } }; +var btcAuth = btcUri.auth ? btcUri.auth.split(':') : []; + +var ifxUri = process.env.BTCEXP_INFLUXDB_URI ? url.parse(process.env.BTCEXP_INFLUXDB_URI, true) : { query: { } }; +var ifxAuth = ifxUri.auth ? ifxUri.auth.split(':') : []; +var ifxActive = !!process.env.BTCEXP_ENABLE_INFLUXDB || Object.keys(process.env).some(k => k.startsWith('BTCEXP_INFLUXDB_')); module.exports = { rpc: { - host: process.env.BTCEXP_BITCOIND_HOST || "127.0.0.1", - port: process.env.BTCEXP_BITCOIND_PORT || 8332, - username: process.env.BTCEXP_BITCOIND_USER, - password: process.env.BTCEXP_BITCOIND_PASS, - cookie: process.env.BTCEXP_BITCOIND_COOKIE || path.join(os.homedir(), '.bitcoin', '.cookie'), + host: btcUri.hostname || process.env.BTCEXP_BITCOIND_HOST || "127.0.0.1", + port: btcUri.port || process.env.BTCEXP_BITCOIND_PORT || 8332, + username: btcAuth[0] || process.env.BTCEXP_BITCOIND_USER, + password: btcAuth[1] || process.env.BTCEXP_BITCOIND_PASS, + cookie: btcUri.query.cookie || process.env.BTCEXP_BITCOIND_COOKIE || path.join(os.homedir(), '.bitcoin', '.cookie'), }, influxdb:{ - active: !!process.env.BTCEXP_ENABLE_INFLUXDB, - host: process.env.BTCEXP_INFLUXDB_HOST || "127.0.0.1", - port: process.env.BTCEXP_INFLUXDB_PORT || 8086, - database: process.env.BTCEXP_INFLUXDB_DBNAME || "influxdb", - username: process.env.BTCEXP_INFLUXDB_USER || "admin", - password: process.env.BTCEXP_INFLUXDB_PASS || "admin" + active: ifxActive, + host: ifxUri.hostname || process.env.BTCEXP_INFLUXDB_HOST || "127.0.0.1", + port: ifxUri.port || process.env.BTCEXP_INFLUXDB_PORT || 8086, + database: ifxUri.pathname && ifxUri.pathname.substr(1) || process.env.BTCEXP_INFLUXDB_DBNAME || "influxdb", + username: ifxAuth[0] || process.env.BTCEXP_INFLUXDB_USER || "admin", + password: ifxAuth[1] || process.env.BTCEXP_INFLUXDB_PASS || "admin" }, // optional: enter your api access key from ipstack.com below diff --git a/bin/cli.js b/bin/cli.js index b169179..83a5654 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -9,6 +9,7 @@ const args = require('meow')(` -l, --login protect web interface with a password [default: no password] --coin crypto-coin to enable [default: BTC] + -b, --bitcoind-uri connection URI for bitcoind rpc (overrides the options below) -H, --bitcoind-host hostname for bitcoind rpc [default: 127.0.0.1] -P, --bitcoind-port port for bitcoind rpc [default: 8332] -c, --bitcoind-cookie path to bitcoind cookie file [default: 8332] @@ -23,7 +24,9 @@ const args = require('meow')(` --sentry-url sentry url [default: disabled] --enable-influxdb enable influxdb for logging network stats [default: false] + --influxdb-uri connection URI for influxdb (overrides the options below) --influxdb-host hostname for influxdb [default: 127.0.0.1] + --influxdb-port port for influxdb [default: 8086] --influxdb-user username for influxdb [default: admin] --influxdb-pass password for influxdb [default: admin] --influxdb-dbname database name for influxdb [default: influxdb] @@ -32,16 +35,22 @@ const args = require('meow')(` -h, --help output usage information -v, --version output version number - Example + Examples $ btc-rpc-explorer --port 8080 --bitcoind-port 18443 --bitcoind-cookie ~/.bitcoin/regtest/.cookie $ btc-rpc-explorer -p 8080 -P 18443 -c ~/.bitcoin/regtest.cookie - All options may also be specified as environment variables: + Or using connection URIs + $ btc-rpc-explorer -b bitcoin://bob:myPassword@127.0.0.1:18443/ + $ btc-rpc-explorer -b bitcoin://127.0.0.1:18443/?cookie=$HOME/.bitcoin/regtest/.cookie + $ btc-rpc-explorer --influxdb-uri influx://bob:myPassword@127.0.0.1:8086/dbName + + All options may also be specified as environment variables $ BTCEXP_PORT=8080 BTCEXP_BITCOIND_PORT=18443 BTCEXP_BITCOIND_COOKIE=~/.bitcoin/regtest/.cookie btc-rpc-explorer + `, { flags: { port: {alias:'p'}, login: {alias:'l'} - , bitcoindHost: {alias:'H'}, bitcoindPort: {alias:'P'}, bitcoindCookie: {alias:'c'} - , bitcoindUser: {alias:'u'}, bitcoindPass: {alias:'w'} + , bitcoindUri: {alias:'b'}, bitcoindHost: {alias:'H'}, bitcoindPort: {alias:'P'} + , bitcoindCookie: {alias:'c'}, bitcoindUser: {alias:'u'}, bitcoindPass: {alias:'w'} , demo: {type:'boolean'}, enableInfluxdb: {type:'boolean'}, nodeEnv: {alias:'e', default:'production'} } } ).flags;