@codemaniac-technologies/codemaniac-config
v1.0.0
Published
> CODEMANIAC Microservice Configuration module used to configure API services and > load environment specific configuration files.
Downloads
5
Readme
codemaniac-config
CODEMANIAC Microservice Configuration module used to configure API services and load environment specific configuration files.
Installation
- Install as an NPM package from its git repository:
npm install https://github.com/CodeManiac-Technologies-Pvt-Ltd/codemaniac-config.git
Dependencies
codemaniac-servermodule:Thie
codemaniac-configmodules works hand in hand with the codemaniac-server module. Though it can be imported independently, thecodemaniac-servermodule automatically handles loading configurations for you.Config files: In the context of this module, there are two classes of config files being used in our projects:
- Environment (dotenv) configs: These configuration files are used to build a set of process environment variables scoped to the environment the application is running in. An environment is denoted by the combination of the NODE_ENV variable (eg. dev, devgate, sqa, prod, etc.) and the client. In essence, the environment identifies the cluster the application is deployed to. The content of these config files generally includes usernames and secrets. And may also include other environment scoped information such as api urls.
- Application (json) configs:
These configuration files are specific to an application or service. These configs focus on the different
configuration options scoped to the application itself. It includes the bulk of the options that are
instantiated by the
codemaniac-servermodule, including:- Which services/modules to load
- Logging capabilities
- General config data for use throughout the application
Specific config keys are shown below and full examples of the configuration files are included in the
config folder.
Most configuration options are provided and maintained in the modules we utilize for development. For simplicity, include below is an amalgamation of serveral options currently provided in our modules and how the may be used. The options shown only serve as an example of what's available and should not be considered the official source documentation for module configurations. To obtain the actual documentation for a given option, please see the corresponding module.
codemaniac-server
Configuration Options:
host: Host object; encapsulates details occurred with the node.js server.Properties
Example
"host": { "port": 3000 }services:The services object encapsulates configuration deatils for a module. Properties are module specific. As it pertains to the
codemaniac-server, the object is iterated through and each service/module is imported and instantiated through either aninitorinitializemethod as defined on the imported object. The config element/object is passed to theinitializemethod, which creates and returns a corresponding instance of itself with the configuration details as passed.NOTE: GENERATED module instances are often expected to be shared as singletons across the application (e.g we use a single logger instance across all of our modules). If this functionality is need, ensure the module initializing the singleton is only added as dependency (
package.json) in thearia-servermodule so it's not otherwise loaded locally in the module that depends upon it.Properties
Example
"services":{ "helpearn": { "module: "codemaniac-ds-mysql", "poolAlias": "helpearn", "modelPaths": "helpearn", "user": "process.env.MYSQLDB_USER", "password": "process.env.MYSQLDB_PASSWORD", "connectionString": "process.env.MYSQLDB_CONNECTIONSTRING", "poolIncrement": 5, "poolMin": 10, "poolMax": 40, "poolTimeout": 10, "queueTimeout": 0, "queueRequests": true, "_enableStats": true } }
featuresThe
featuresobject includes additional details used to configure the server instance.Properties
"features": { "logRequests": true, "logErrors": true, "healthCheck": true, "healthCheckDiagnostics": true, "errorDiagnostics": true }
codemaniac-logger:
Configuration Options:
logger: Logger configuration object.
API
Object: codemaniacConfig
codemaniacConfig object is exported when requiring/importing the module.
const codemaniacConfig = require('codemaniac-config');- Returns: {codemaniacConfig}
Methods/Functions
module.exports.loadLocalEnvironment = loadLocalEnvironment; module.exports.loadLocalAppConfig = loadLocalAppConfig;
codemaniacConfig.get(key)
Retreives a configuration value from memory by key
key{string} - the key to retrevice, colon delimitted to denote nested object (e.g.'services:helpearn:module'gets theservices.helpearn.modulevalue)- Returns: {Object/string}
codemaniacConfig.set(key, value)
key{string} - the key for which to set the value to, colon delimitted to denote nested object (e.g.'services:helpearn:module'sets theservices.helpearn.modulevalue)value{string} - the value to set the key to- Returns: void
codemaniacConfig.add(name, configObject)
Adds an additional object to the in-memory config object store. Overrides key if already defined.
name{String} - Name identifier for the configuration object in the store.configObject{object} - The configuration object- Returns: void
codemaniacConfig.load([localFileConfig] = {})
localFileConfig{Object} - Configuration object providing local file paths to configslocalFileConfig.tls{Object} - Tls configuration object providing local file paths to tls fileslocalFileConfig.tls.certPath{string} - Path to tls cert filelocalFileConfig.tls.keyPath{string} - Path to tls key file
Loads base configuration along with additional configurations files and data (e.g. from s3 or SSM) based on the base options passed. NOTE: Local env and application config files will be loaded if the following environment variables are set:
process.env.LOCAL_ENV_CONFIG_PATH{string} - Path to environment (.env) fileprocess.env.CODEMANIAC_LOCAL_APP_CONFIG_PATH{string} - Path to application config
codemaniacConfig.loadEnvConfig()
Loads .env file into memory or from SSM.
- Returns: void
codemaniacConfig.loadEnvConfig.exposeEnvConfig()
Loads .env file key/value pairs into memory as process environment variables.
- Returns: void
codemaniacConfig.loadAppConfig.subConfigEnvVariables(configObject)
Substitutes harcoded process environment variable strings with their in-meory values
configObject{Object} - The configuration object- Returns: {object} - The updated configObject with variables substituted.
codemaniacConfig.loadLocalAppConfig([configFilePath])
Loads application config.json file as specified by configFilePath into memory as a configuration object.
configFilePath{string} - path to the application config.json file -optional- Returns: void
codemaniacConfig.getSSMParameters(parameters, [withDecryption])
Retreives SSM parameteres as requested.
parameters{Array} - the configuration objectwithDecryption{boolean} - Option to decrypt secure string parameters. Defaults to false.Returns: {Promise} - The parameters object. See AWS documentation for specific details.
Usage
- Include the following in your code files:
const codemaniacConfig = require('codemaniac-config');
const configObject = {
services: {
dataprotect: {
clientKeySpecs: {
default: {
currentSpec: 'key-spec-value',
},
},
},
},
};
// add the configObject
ariaConfig.config.add('local-config', configObject);
//get a key value ("key-spec-value")
const keySpec = codemaniacConfig.config.get(
'services:dataprotect:clientKeySpecs:default:currentSpec'
);
//set a value
const newKeySpec = codemaniacConfig.config.set(
'services:dataprotect:clientKeySpecs:default:currentSpec',
'new-key-spec-value'
);Tests
- To run the test suite, first install the dependencies the run
npm test
npm install
npm testAn HTML copy of the coverage report will be written to the ./coverage directory.
- Execute tests without code coverage reports:
npm run test-only- Check adherance to style guidelines and detect potential problems:
npm run lint- Check for known security exploits of dependent packages:
npm run security