1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-30 10:10:44 +02:00
go-micro/config/source/env
2022-09-30 20:32:55 +02:00
..
env_test.go go-micro.dev/v4 (#2305) 2021-10-12 12:55:53 +01:00
env.go fix: some linting issues (#2563) 2022-09-30 16:27:07 +02:00
options.go fix: linting issues (#2566) 2022-09-30 20:32:55 +02:00
README.md fix: linting issues (#2566) 2022-09-30 20:32:55 +02:00
watcher.go go-micro.dev/v4 (#2305) 2021-10-12 12:55:53 +01:00

Env Source

The env source reads config from environment variables

Format

We expect environment variables to be in the standard format of FOO=bar

Keys are converted to lowercase and split on underscore.

Format example

DATABASE_ADDRESS=127.0.0.1
DATABASE_PORT=3306

Becomes

{
  "database": {
    "address": "127.0.0.1",
    "port": 3306
  }
}

Prefixes

Environment variables can be namespaced so we only have access to a subset. Two options are available:

WithPrefix(p ...string)
WithStrippedPrefix(p ...string)

The former will preserve the prefix and make it a top level key in the config. The latter eliminates the prefix, reducing the nesting by one.

Prefixes example

Given ENVs of:

APP_DATABASE_ADDRESS=127.0.0.1
APP_DATABASE_PORT=3306
VAULT_ADDR=vault:1337

and a source initialized as follows:

src := env.NewSource(
    env.WithPrefix("VAULT"),
    env.WithStrippedPrefix("APP"),
)

The resulting config will be:

{
  "database": {
    "address": "127.0.0.1",
    "port": 3306
  },
  "vault": {
    "addr": "vault:1337"
  }
}

New Source

Specify source with data

src := env.NewSource(
  // optionally specify prefix
  env.WithPrefix("MICRO"),
)

Load Source

Load the source into config

// Create new config
conf := config.NewConfig()

// Load env source
conf.Load(src)