node-microsvc-lib
Installation
npm install node-microsvc-lib --save
Usage
Creating a microservice app:
app.ts
"use strict"; ; // factories/modules;;; // configs; const logger = ; // create microservice appvconst app = configs logger; app; app;app;app; process; app;
How configuration works
The Microservice instance expects a ServiceConfigs instance.
This ServiceConfigs instance requires the base dir __dirname
, an instance of AppBaseConfigs and an (optional) instance of IConfigsProvider.
Three sets of configuration values exist:
- Parameters - values can be of type STRING, BOOL, INT_NUMBER or FLOAT_NUMBER;
- Feature Flags - values are always of boolean type;
- Secrets - values are always of string type.
ServiceConfigs (Required for Microservice obj instantiation)
This is the object that the Microservice requires to source all its runtime configurations.
IConfigsProvider (Optional)
Optional instance that fetches all config values from an external service such as consul or hashicorp vault.
Load order / precedence
The order of loading:
- params.js file - ServiceParams instance gets loaded along with the default values;
- params.ENV_NAME.js file - the one that overrides values in ServiceParams per env;
- IConfigsProvider - if a correspondent key name exists the value from the provider overrides the current one (after app/microservice init);
- Environment vars - all parameters can be overridden by passing an uppercase env var with the param key name (after app/microservice init)
In summary, env vars always win (if defined).
Minimum config structure
src/config/config.ts (main config object)
src/config/param.ts (default params definition)
Example of config code structure
config/config.ts
"use strict"; ; let app_base_confs = ;app_base_confsenv = processenvAPP_ENV || 'dev_local';app_base_confssolution_name = "my_solution";app_base_confsapp_name = "my_app";app_base_confsapp_version = "0.0.1";app_base_confsapp_api_prefix = "";app_base_confsapp_api_version = "1"; ;
config/params.ts
"use strict"; ;let params = ; params; params; params; ;
config/overrides.stage.ts (optional file see step 2 above - where "stage" comes from APP_ENV)
"use strict"; ; module{ // override params, feature_flags or secrets values' configs; };
Pre-requisites for contributing
NVM - Node Version Manager - https://github.com/creationix/nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
After NVM is installed, go to the proect directory and execute this to download and install the correct node version:
nvm install -s