Skip to content

Strongly typed config for golang apps along with a viper implementation.

License

Notifications You must be signed in to change notification settings

theflyingcodr/goconfig

Repository files navigation

Release Build Status Report Go Mergify Status

goconfig

GoConfig provides strongly typed envrionment variable based configuration for go apps using a friendly Fluent API.

It comes with a viper implementation but others that implement the ConfigurationLoader interface can also be added.

Though it is billed as environment variable based, you can load configuration in however you wish, try to keep it 12 factor though with env vars.

Useage

Useage is very simple, at the start of your main first load your defaults, I tend to have a method like SetupDefaults that contains all the default settings for all environment variables in the application. Directly after this, call goconfig as shown:

func main(){
	config.SetupDefaults()
	cfg := goconfig.NewViperConfig("my-app").
		WithServer().
		WithDeployment().
		WithDb().
		Load()
}

With this simple syntax you can quickly glance at your main file and see the exact requirements for your application.

You can then reference configs directly rather than plucking them out of thin air throughput your code base.

For example, to get the server port do:

 svr := cfg.Server.Port

It is recommended that you use dependency injection rather than globals to pass these around, as such, they are all pointers.

 func NewImportantService(cfg *goconfig.Server){
	 cfg.Port
}

The above injection akes it 100% explicit as to the requirements of your service and is much easier tested than putting config readers throughout your code base.

Http Clients

We also support setup of custom http clients, this can be done as shown:

func main(){
	config.SetupDefaults()
	cfg := goconfig.NewViperConfig("my-app").
		WithServer().
		WithHTTPClient("my-service").
		WithHttpClient("my-other-service").
		Load()
}

Each of these gets its own config keyed off the service name as shown:

	EnvHTTPClientHost       = "%s.client.host"
	EnvHTTPClientPort       = "%s.client.port"
	...

In the case of my-service, it's environment config for host would be MY-SERVICE.CLIENT.HOST.

You can add as many as you need and can access them by calling svcCfg := cfg.CustomHTTPClient("my-service).

Contributing

Contributions are more than welcome, there is a limited set of configs available at present and I'll be adding them as I need them, so if you think you'd like to use this library but has something missing, like a Cassandra config, either raise an issue, create a PR adding it or both ideally.

About

Strongly typed config for golang apps along with a viper implementation.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published