Skip to content

JSON & YAML based configuration library with automatic reload

License

Notifications You must be signed in to change notification settings

dparrish/go-autoconfig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Documentation Build Status

go-autoconfig

JSON & YAML based configuration library with automatic reload.

Example Usage

import "github.com/dparrish/go-autoconfig"

func main() {
	// Load the initial configuration.
	config := autoconfig.New("config.yaml")

	// Ensure that hash.value.here is always set in configuration, even after it changes.
	config.Required("hash.value.here")

	// Ensure that hash.value.cannot.change doesn't change on configuration reload.
	config.Immutable("hash.value.cannot.change")

	// Validate values whenever configuration is loaded.
	config.AddValidator(func(old, new *autoconfig.Config) error {
		if new.GetInt("intvalue") > 1000 {
			return errors.New("intvalue cannot be above 100")
		}
		return nil
	})

	if err := config.Load(); err != nil {
		panic("Configuration is invalid")
	}

	// Start a background goroutine that will reload configuration whenever it changes.
	if err := config.Watch(context.Background()); err != nil {
		panic(err)
	}

	...

	// Read config values in your code. These will always return the latest loaded value.
	log.Printf("String value: %s", config.Get("hash.stringvalue"))
	log.Printf("Int value: %d", config.GetInt("hash.intvalue"))
	log.Printf("List value: %v", config.GetAll("hash.listvalue"))
}

About

JSON & YAML based configuration library with automatic reload

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages