Record a CONFIDENCE plug-in that simply changes configurations based on different environment variables. When writing config or Settings files, you need to carefully distinguish the environment. Parameter values are confusing and difficult to manage. With the CONFIDENCE plug-in, you don’t have to worry about confusing environment parameters anymore. Keep updating smilejoe.cn

Confidence address github.com/hapipal/con…

The basic structure

The structure is clear, with different values for different environment variables. And you can do some range control. It is easy to use, as long as the standard parameter (such as :$filter) is configured.

{
    "key1": "abc"."key2": {
        "$filter": "env"."production": {
            "deeper": {
                "$value": "value"}},"$default": {
            "$filter": "platform"."android": 0."ios": 1."$default": 2}},"ab": {
        "$filter": "random.a"."$range": [{"limit": 10."value": 4 },
            { "limit": 20."value": 5}]."$default": 6
    },
    "$meta": {
        "description": "example file"}}Copy the code

API

Before using CONFIDENCE, let’s take a look at its API

New Store(document) creates a configuration container store.load(document) loads a configuration file store.get() generates the configuration based on the parameters

// A simple demo
const { Store } = require('confidence')
const { env } = process
// Take the value from the environment variable
const criteria = {
  env: env.NODE_ENV
}
const settings = {
  platform: {
    $filter: 'env'.development: 'this is DEV environment'.qa: 'this is in QA environment'.production: 'this is in PRO environment'.$default: 'this is in LOCAL environment'}}// Create a configuration container
const store = new Store(settings)

module.exports = {
  settings: store.get('/', criteria) // Generate the corresponding configuration based on the environment variables
}
// Settings {platform: 'this is in DEV environment'}
Copy the code

Standard parameters

filter

Rule: Use only letters and ‘_’ for variable names. Choose which variable to use based on the value of the matching environment variable. (See a moment to understand)

platform: {
  $filter: 'NODE_ENV'.development: 'this is DEV environment'.qa: 'this is in QA environment'.production: 'this is in PRO environment'.$default: 'this is in LOCAL environment'
}
Copy the code

Run export on the terminal to print all environment variables. Check the value of NODE_ENV. The local value of NODE_ENV is dev, so use default. {platform: ‘this is in LOCAL environment’}

PS: $filter is a switch-case statement.

range

Serve chestnuts first

key: {
    "$filter": "random"."$range": [{"limit": 10."value": 4 },
        { "limit": 20."value": 5}]."$default": 6
}
Copy the code

When random is less than or equal to 10, the key value is 4. When random is greater than 10 and less than or equal to 20, the key value is 5. When random is greater than 20, the key value is 6

Shared values ($base)

Illustrate with chestnuts

{
  "$filter": "env"."$base": {
      "logLocation": "/logs"
  },
  "production":  {
      "logLevel": "error"
  },
  "qa":  {
      "logLevel": "info"."logLocation": "/qa/logs"
  },
  "staging":  {
      "logLevel": "debug"}}Copy the code

When $base is written, there is a default “logLocation”: “/logs” attribute if logLocation is not overridden. So when env is production, the result is

{ 
  "logLevel": "error"."logLocation": "/logs"
}
Copy the code

When env is QA, the result is

{
  "logLevel": "info"."logLocation": "/qa/logs"
}
Copy the code

conclusion

Using CONFIDENCE to set a configuration file is quick and clear, and can be used with any Node framework (HAPI or KOA).

The last

Hapi is a small scaffold with confidence, Glue, etc. Demo portal github.com/JoeSmile/ha…