From b88214d5a7c6c6d1e93748acacfe41fe85c593b6 Mon Sep 17 00:00:00 2001 From: David Grochowski Date: Sat, 9 Apr 2016 09:23:54 -0400 Subject: [PATCH 1/2] Ignore vim swapfiles. --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 100a781..58ca6de 100644 --- a/.gitignore +++ b/.gitignore @@ -33,4 +33,6 @@ node_modules .node_repl_history .aws_configure -.idea \ No newline at end of file +.idea +*.swo +*.swp From 67365a85cf2379b3eb87e2cedfc34c47cea9dbc2 Mon Sep 17 00:00:00 2001 From: David Grochowski Date: Sat, 9 Apr 2016 09:24:14 -0400 Subject: [PATCH 2/2] Add command line arguments for overriding the bind host/port and for manually specifying the region. --- index.js | 65 ++++++++++++++++++++++++++++++++++++++++++---------- package.json | 3 ++- 2 files changed, 55 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index 3f0bd78..94f10ca 100644 --- a/index.js +++ b/index.js @@ -8,20 +8,61 @@ var bodyParser = require('body-parser'); var stream = require('stream'); var figlet = require('figlet'); -if (process.argv.length != 3) { - console.error('usage: aws-es-proxy '); +var yargs = require('yargs') + .usage('usage: $0 [options] ') + .option('b', { + alias: 'bind-address', + default: '127.0.0.1', + demand: false, + describe: 'the ip address to bind to', + type: 'string' + }) + .option('p', { + alias: 'port', + default: 9200, + demand: false, + describe: 'the port to bind to', + type: 'number' + }) + .option('r', { + alias: 'region', + demand: false, + describe: 'the region of the Elasticsearch cluster', + type: 'string' + }) + .help() + .version() + .strict(); +var argv = yargs.argv; + +if (argv._.length !== 1) { + yargs.showHelp(); process.exit(1); } -var ENDPOINT = process.argv[2]; -var m = ENDPOINT.match(/\.([^.]+)\.es\.amazonaws\.com\.?$/); -if (!m) { - console.error('region cannot be parsed from endpoint address, must end in ..es.amazonaws.com'); - process.exit(1); + +var ENDPOINT = argv._[0]; + +// Try to infer the region if it is not provided as an argument. +var REGION = argv.r; +if (!REGION) { + var m = ENDPOINT.match(/\.([^.]+)\.es\.amazonaws\.com\.?$/); + if (m) { + REGION = m[1]; + } else { + console.error('region cannot be parsed from endpoint address, etiher the endpoint must end ' + + 'in ..es.amazonaws.com or --region should be provided as an argument'); + yargs.showHelp(); + process.exit(1); + } +} + +var TARGET = argv._[0]; +if (!TARGET.match(/^https?:\/\//)) { + TARGET = 'https://' + TARGET; } -var REGION = m[1]; -var TARGET = 'https://' + process.argv[2]; -var PORT = 9200; -var BIND_ADDRESS = '127.0.0.1'; + +var BIND_ADDRESS = argv.b; +var PORT = argv.p; var creds; var chain = new AWS.CredentialProviderChain(); @@ -83,4 +124,4 @@ console.log(figlet.textSync('AWS ES Proxy!', { })); console.log('AWS ES cluster available at http://' + BIND_ADDRESS + ':' + PORT); -console.log('Kibana available at http://' + BIND_ADDRESS + ':' + PORT + '/_plugin/kibana/'); \ No newline at end of file +console.log('Kibana available at http://' + BIND_ADDRESS + ':' + PORT + '/_plugin/kibana/'); diff --git a/package.json b/package.json index ac75abf..7240035 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "body-parser": "^1.15.0", "express": "^4.13.4", "figlet": "^1.1.1", - "http-proxy": "^1.13.2" + "http-proxy": "^1.13.2", + "yargs": "^4.6.0" } }