Skip to content

healthimation/go-env-config

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

go-env-config

This library is a shim to allow configuration from environment variables to satisfy the interfaces for config using go-consul-client. The constructs are specifically tailored to allow minimal changes to services already using go-consul-client, but allow us to run on Heroku-like platforms, as well as keeping it easy to migrate back to an environment using Consul.

It is expected that something external to the services is managing the available environment variables, and the apps are just consumers.

Usage

Application

There is no standalone usage for this library.

Library

You can import this library into you golang application and then use it to access your environment configuration:

To fetch configuration:

import "github.com/healthimation/go-env-config/client"


func main() {

  conf = client.NewEnvLoader()

	//fetch data from the cache
	myConfigString := conf.MustGetString("ENV_VAR")
	myConfigBool := conf.MustGetBool("ENV_VAR")
	myConfigInt := conf.MustGetInt("ENV_VAR")
	myConfigDuration := conf.MustGetDuration("ENV_VAR")

	...
}

The balancer package returns the same types as the Consul balancer package, but just determines its information from the environment.

import "github.com/healthimation/go-env-config/balancer"


func main() {

  serviceName := "authentication"

  // use PGHOST:PGPORT env vars to find the DB, and append "_URL" to service name lookups
  // pass in different env var names for db host and port if required
  balancer = client.NewEnvBalancer("PGHOST", "PGPORT", "_URL")

  // setup db
	dbLoc, err := balancer.FindService(fmt.Sprintf("%s-db", serviceName))
	if err != nil {
		...
	}

  // use the db
	db, err := data.NewDb("postgres", fmt.Sprintf("postgres://%s:%s@%s:%d/authentication?sslmode=disable", dbUser, dbPass, dbLoc.URL, dbLoc.Port))
	...

  // find a service
  loc, err := lb.FindService("secure-code") // looks up SECURE_CODE_URL in environment
	if err != nil {
	   ...
  }

}

About

A configuration package that pulls from environment variables

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages