Consulea
Load Consul keys, environment vars, and command line arguments in a predictable, standardized way.
Module goals:
- Compile single config object from Consul kv, environment variables, and command line arguments.
- Watch for changes in Consul prefix, send an event with updated config.
- Verify required variables are set.
- Be extremely light weight, having minimal dependencies.
- Predictable config key outcome given different standards of input variables using different sources.
- Simplify origin of keys in Consul and Env. Each project should have it's own namespace, nothing shared.
- No ES6 dependency, so it works under Node.js 0.10 and onward.
Variables are first read from Consul, then the environment, then command line arguments, allowing the user to override something already previously set in Consul. The config key webPort
can be set by Consul key test-svc/web-port
and can be overridden by environment variable TESTSVC_WEB_PORT
, and both can be overridden by --web-port
.
Example
var Consulea = require('consulea');
var consulea = new Consulea({
consulToken: '4fe3dee9-4148-404e-9928-d95cfb1e6947',
consulPrefix: 'test-svc/',
envPrefix: 'TESTSVC',
requiredKeys: ['serverName', 'port']
});
var myConfig = {};
consulea.on('change', function (err, data) {
myConfig = data;
});
consulea.on('ready', function (err, data) {
myConfig = data;
});
Config is now available to project from three different sources:
Consul key | Env key | CLI key | Resulting variable |
---|
test-svc/port | TESTSVC_PORT | --port | port |
test-svc/server-name | TESTSVC_SERVER_NAME | --server-name | serverName |
test-svc/max-connects | TESTSVC_MAX_CONNECTS | --max-connects | maxConnects |
test-svc/time-out-ms | TESTSVC_TIME_OUT_MS | --time-out-ms | timeOutMs |
export TESTSVC_MAX_CONNECTS=100
export TESTSVC_SERVER_NAME="New staging server"
node someScript.js
TESTSVC_MAX_CONNECTS=100 TESTSVC_SERVER_NAME="New staging server" node someScript.js
node someScript.js --max-connects=50 --server-name="Prod server"
Config object options
Key | Required | Description |
---|
consulToken | No | ACL Token used to authenticate with Consul service |
consulPrefix | Yes | Namespace/prefix to search for keys in Consul |
envPrefix | No | Namespace/prefix to search for keys in local environment |
requiredKeys | No | List of camelCased keys which must exist, or script will exit |
exitIfRequiredKeysFail | No | If set false , Consulea will just warn about missing key |
consulClientConfig | No | Consul config object, if you'd rather configure it yourself |
Notes
The Consul host:port can be also be defined using the environment variable CONSUL_HTTP_ADDR
. This may be helpful if code is running in Docker or the Consul agent isn't running on the local instance.
export CONSUL_HTTP_ADDR='https://remoteconsulserver.com:8500'
node someScript.js