Skip to content

About

JSON & YAML based configuration library with automatic reload

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

12 Commits

Folders and files

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

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages

⌂ 换网址