Config File

appcfg.yaml is a file containing the settings of the modules in the application
Setting is the sum of the config in the application

Why ?

  • Easy to customize modules

  • Flexible and versatile

  • Easy to learn and easy to use

  • Allow inheritance

  • Support deep reference

  • Support internal reference

  • Support external reference

  • Support value lock

Usage

The config can be divided into two categories by level: Module Level and Application Level

Module Level

1. Services config

service-name
├── controller/
├── model/
├── index.js
├── router.js
└── appcfg.yaml
appcfg.yaml
page_size: 20
filter:
- age
- location

Service config can be accessed by the this.$config variable

Normal services
Unofficial service
Normal services
getUsers(){
var {page_size, filter} = this.$config;
...
}
Unofficial service
function handler(app, cfg){
var {page_size, filter} = this.$config;
...
}

2. Plugins config

plugin-name
├── src/
├── lib/
├── index.js
├── hyron-plugin.js
└── appcfg.yaml
./plugins/demo/appcfg.yaml
private_key: 123456
./plugins/demo/hyron-plugin.js
function handler(req, res, prev, cfg){
var {private_key} = cfg;
...
}

3. Addons config

addon-name
├── src/
├── lib/
├── index.js
├── hyron-addon.js
└── appcfg.yaml
./addons/demo/appcfg.yaml
email: thangphung.work@gmail.com
function handler(cfg){
var {private_key} = cfg;
...
}

Application Level

root
├── services/
├── addons/
├── plugins/
├── res/
├── server/
├── public/
├── index.js
├── package.json
└── appcfg.yaml
./appcfg.yaml
public_key: ahihi123
app_name: myApp

Features

1. Lock field

To prevent inheriting a field, add '$' before the name of the field. For example

./plugins/demo/appcfg.yaml
$private_key: 123456

2. Flexible access

You can access the config anywhere in your project while running, use getConfig(..)

./addons/crud/appcfg.yaml
schema:
accounts:
email: string
password: string
./addons/crud/hyron-addons.js
const {getConfig} = require("hyron");
module.exports = function(cfg){
var schemaList = getConfig("schema", {});
var accountModel = getConfig("schema.accounts", {});
var emailType = getConfig("schema.accounts.email", {});
...
}

3. Internal reference

The value of a field in appcfg can be referenced to another field. For example

appcfg.yaml
key1:
child1: hello
key2: <#key1.child1> # hello

4. External reference

The value of a field in appcfg can be referenced externally by another yaml file

./models/account.yaml
email: string
password: string
./addons/crud/appcfg.yaml
schema:
account: <~models/account.yaml>

5. Allow inheritance

appcfg.yaml in Application Level can override the config from the config in Module Level by identifying the module (declared in the build file)

./appcfg.yaml
service_name:
page_size: 50
addons_name:
email: hyron.dev@gmail.com
plugins_name:
private_key: abc123