Skip to content

jfbus/autoconfig

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
ini
 
 
 
 
 
 
 
 
 
 
 
 

autoconfig Build Status

Autonomous configuration for golang packages with hot reload.

  • Each package has its own configuration section within a single global config file, neither main() nor any other part of your application has the knowledge of the package configuration.
  • Config can be dynamically updated when the application receives a signal.

Supported file format are :

Usage (YAML)

Init :

autoconfig.Load(yaml.New(filename))
autoconfig.ReloadOn(syscall.SIGHUP)

Sample config file :

[...]

section_name:
  group:
    value: foobar

[...]

Package config :

package mypackage

type GroupConfig struct {
	Value `yaml:"value"`
}

type PkgConf struct {
	Group GroupConfig `yaml:"group"`
}

func (c *PkgConf) Changed() {
	// Do something when config has changed
}

var (
  // config, with default values
	pkfCong = PkgConf{
		Group: GroupConfig{
			Value: "default value",
		},
	}
	_ = autoconfig.Register("section_name", &pkgConf)
)

Instance config :

package mypackage

var (
	// Set defaults
	_ = autoconfig.Register("section_name", &PkgConf{
		Group: GroupConfig{
			Value: "default value",
		},
	})
)

type PkgClass struct {}

func New() *PkgClass {
	n := &PkgClass{}
	// This will trigger a n.Reconfigure() call with the current config
	autoconfig.Reconfigure("section_name", n)
	return n
}

func (c *PkgClass) Reconfigure(c interface{}) {
	if cfg, ok := c.(*PkgConf); ok {
		// Do something when config has changed
	}
}

autoconfig will cleanly Lock/Unlock your structs provided they implement sync.Locker

Usage (INI)

Init :

autoconfig.Load(ini.New(filename))
autoconfig.ReloadOn(syscall.SIGHUP)

Sample config file :

[...]

[section_name]
value=foobar

[...]

Package config :

package mypackage

type PkgConf struct {
	Value string `ini:"value"`
}

var (
	pkfCong = PkgConf{
		Value: "default value",
	}
	_ = autoconfig.Register("section_name", &pkgConf)
)

Other file formats

Any config file format can be used, provided a loader class implementing the Loader interface is provided :

type Loader interface {
	Load(map[string]interface{}) error
}

Caveats

  • Only a single config file is supported,
  • Values types are supported only if the underlying format supports them (e.g. INI does not support slices).

TODO

  • Multiple files

License

MIT - see LICENSE

About

Autonomous configuration for golang packages with hot reload

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages