The config app manages your project's configuration values, which are
read from the projects configuration directory. By default, the config
directory is config/, but you can change that by specifying CONFIG_DIR
in your .env file.
The config directory should contain an app.js at a minimum,
and additionally other config files with names corresponding to
particular apps that use them. Each app documents its configuration
requirements.
In addition to reading app.js, the config service will read any file from
the configuration directory whose name matches an installed app.
If you want to add custom configuration values for use in your application,
you are free to add other keys to the object exposed by app.js. If you are
writing custom apps, you can give them their own configuration files too.
The config service is accessible from the App instance or from another
service.
// before mounting has occurred
const ConfigSvc = App.$('config');
// after mounting has ocurred
const ConfigSvc = App.$config;
// from within a service method after loading has occured
const ConfigSvc = this.$config;
The config service reads the config files when the config app is loaded.
The config app is the first app to load.
The config service provides a
get(appName, key, defaultValue=null) method to read configuration
values.
The following snippet will return the value of port in config/http.js:
const serverPort = App.$config.get('http', 'port', 8080);
The get method will return one of the four following things (in order of precedence):
port) property defined in the specified config file (ie, config/http.js), if it exists.port) property defined in the internal config.js file of the specified app, if it exists.8080), if provided.null is returned.Note that all Carolina apps that expose configuration files have defaults
defined in an internal config.js file for that app, so when trying to get
the value of those configuration settings the default value
provided to the method call will never be
returned.
The config app exposes a CLI command group 'config' with a command
for checking a configuration value.
node . config value <app> <config_key>
This command lets you check the value of a configuration setting. For example,
to check what the config service is tracking for the port value of the
http configuration, run the following command from project root:
$ node . config value http port
8080
You can lookup any value this way. You will see the string "<NOT_SET>" in any case where an existing value is not defined.
When you are writing your own apps, whether a custom app in apps/
or your "main" app, you can define default configuration in a config.js
file in your app's directory.
If you are writing apps to be reusable across projects then they should
define a config.js, so that defaults are defined for anyone who uses that
app but doesn't have a config file for it in their project configuration
folder.
There is also a way to make it easy for app users to copy a config file from an app into their project configuration directory, and this will be covered in the section on the "publish" app.
In a custom app called "my_custom_app", you would write a config file in
apps/my_custom_app/config.js. It could look something like this:
module.exports = {
themeColor: "blue"
};
Then, code in your service methods might access that value like this:
App.$config.get('my_custom_app', 'themeColor', 'red'); // returns "blue"
A user of the app could override that theme color by writing the following as
config/my_custom_app.js:
module.exports = {
themeColor: "green"
};