> CODEMANIAC Microservice Configuration module used to configure API services and > load environment specific configuration files.
npm install @codemaniac-technologies/codemaniac-config> CODEMANIAC Microservice Configuration module used to configure API services and
> load environment specific configuration files.
1. Install as an NPM package from its git repository:
``bash`
npm install https://github.com/CodeManiac-Technologies-Pvt-Ltd/codemaniac-config.git
1. codemaniac-server module:
Thie codemaniac-config modules works hand in hand with the codemaniac-server module. Though it can be imported independently, the codemaniac-server module automatically handles loading configurations for you.
1. 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-server module, 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.
#### Configuration Options:
- host:
Host object; encapsulates details occurred with the node.js server.
##### Properties
| Name | Type | Description | Example | |
|---|---|---|---|---|
| port | integer | Application port the service will listen on. If not passed, defaults to 443 | 3000 |
##### Example
`.js`
"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 andinit
instantiated through either an or initialize method as defined on the imported object. The configinitialize
element/object is passed to the method, 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 the aria-server module so it's
not otherwise loaded locally in the module that depends upon it.**
##### Properties
| Name | Type | Description | Example | |
|---|---|---|---|---|
| <"user_defined"> | object | Codemaniac module/services config object containing details used to instantiate the corresponding module instance. The module instance returned on instantiation will be stored under the object name as passed. |
|
##### Example
`.js`
"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
}
}
`
`
- features
The features object includes additional details used to configure the server instance.
##### Properties
| Name | Type | Descriptions | Example |
|---|---|---|---|
| logRequests | boolean | When true, sets up the apiLogger middleware to log requests and response as configured. | true |
| logErrors | boolean | When true, sets up the apiLogger middleware to log API errors. | true |
| healthCheck | boolean | When true, creates a '/healthcheck' route on the server which provides details about the health of the service. | true |
| healthCheckDiagnostics | boolean | When true, returns additional health check information about the server on the '/healthcheck' route. Only used when feature.healthCheck = true. | true |
| errorDiagnostics | boolean | When true, additional error details are returned in the Error response object. | true |
`.js`
"features": {
"logRequests": true,
"logErrors": true,
"healthCheck": true,
"healthCheckDiagnostics": true,
"errorDiagnostics": true
}
#### Configuration Options:
- logger:
Logger configuration object.
codemaniacConfig object is exported when requiring/importing the module.
`js`
const codemaniacConfig = require('codemaniac-config');
- Returns: {codemaniacConfig}
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'services.helpearn.module
gets the value)
- 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 the services.helpearn.module value)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
1. Include the following in your code files:
`js
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'
);
`
1. To run the test suite, first install the dependencies the run npm test
`bash`
npm install
npm test
An HTML copy of the coverage report will be written to the ./coverage directory.
2. Execute tests without code coverage reports:
`bash`
npm run test-only
3. Check adherance to style guidelines and detect potential problems:
`bash`
npm run lint
4. Check for known security exploits of dependent packages:
`bash``
npm run security