Configuration
Configuration is highly flexible and settings can be read from different sources like JSON, environment variables, service class files, etc.
// app.js file
'use strict';
import { Server } from 'kikwit';
const server = new Server();
server.configure(config => {
// Merge settings from the specified json file.
// You can pass a second argument to indicate whether an exception
// should NOT be thrown if the file is missing.
config.addJsonFile('config/default.json'); // Fails if the file is missing
config.addJsonFile(`config/${config.environement}.json`, true); // Doesn't fail if the file is missing
// Merge settings from the specified javascript file.
// You can pass a second argument to indicate whether an exception
// should NOT be thrown if the file is missing.
config.addJavaScriptFile('config/default.js');
config.addJavaScriptFile(`config/${config.environment}.js`, true);
if (config.isEnvironment('development')) {
// Merge user configuration settings.
// More details on services can be found at https://github.com/kikwit/kikwit#user-configuration-tool
config.addUserConfig();
}
// Merge environment variables. A string argument (prefix) can be passed to add only the variables
// that start with the specified prefix. The prefix is removed from the variable name when
// the variable is added to the configuration.
// Use double underscores (__) to create a hierarchy.
// E.g. 'DB__HOST:ALPHA' gets added as { DB: { HOST: 'ALPHA' } }
// config.addEnvironmentVariables();
config.addEnvironmentVariables('APP_');
// Merge the specified object
config.merge({
randomFlag: true,
log: {
level: 'debug',
dest: 'file'
}
});
// Read config entries with `config.get(KEY)`
config.get('log.dest') == 'file';
});
server.start().then(() => {
console.log(`Server started`);
});
To avoid storing sensitive data in your code and source control, you can pass them to your program by the use of environment variables or, when in development, by using the User Configuration tool.
User Configuration tool
The User configuration tool is used to managed user specific configuration settings. This tool is included in the official Kikwit yo generator.
You need to run this too in your project root folder.
To read all settings:
yo kikwit:user-config
To set a setting:
yo kikwit:user-config set db.Host GAMMA
yo kikwit:user-config set db.Port 8885
To read a setting:
yo kikwit:user-config get db
yo kikwit:user-config get db.Port
To remove a setting:
yo kikwit:user-config remove db.Port
To clear all settings:
yo kikwit:user-config clear
Use the following to add user configuration settings in your application:
// app.js file
'use strict';
import { Server } from 'kikwit';
const server = new Server();
server.configure(config => {
// ...
if (config.isEnvironment('development')) {
// Merge user configuration settings.
// More details on services can be found at https://github.com/kikwit/kikwit#user-configuration-tool
config.addUserConfig();
}
// ...
});
server.start().then(() => {
console.log(`Server started`);
});