kelseyhightower / envconfig

Golang library for managing configuration data from environment variables
MIT License
5.01k stars 377 forks source link

Support overlaying environment values onto existing values. #173

Open taothit opened 4 years ago

taothit commented 4 years ago

Use Case

Our common application configuration requires each application to define a small set of descriptive information about the service in order to make the application deployable, discoverable, etc.

envconfig.Process along with default struct tag definitions allow for both global default value configuration as well as environment-specific configuration. However, they do not support defining application-specific defaults for common configuration concerns (application name, e.g.) while preserving global default values and required field behaviors. It's not possible to use default value and setting the same information in each environment is redundant and prone to errors when not setting it correctly in each environment.

envconfig.Overlay enables per-application defaults by replacing only values defined in a configuration struct instance with environment variables and not the default values (if they are defined).

Configuration values will adhere to following order of precedence with these changes:

Configuring a simple web server

Assume we have the provided configuration struct:

type ServerInfo struct {
    http             string `default:":8080"`
    appName          string `required:"true"`
    env              string `default:"local"`
    messageBrokerURL string `default:"localhost:8081"`
}

with the following environment variables defined:

SERVER_INFO_ENV=STAGE
SERVER_INFO_MESSAGE_BROKER_URL=http://message-broker-stage.bigbankcorp.internal/messages/recent

We can define the server info before overlaying values from the environment as follows:

base := ServerInfo{
  appName: "MessageDigester",
}

info := envconfig.Overlay("SERVER_INFO", &base)

Then, we run envconfig.Overlay() and expect info to be represented in json as:

{
  "http": ":8080",
  "appName": "MessageDigester",
  "env": "STAGE",
  "messageBrokerURL": "http://message-broker-stage.bigbankcorp.internal"
}
strideynet commented 4 years ago

👍 Need this