Autonomous configuration for golang packages with hot reload
MIT License
Autonomous configuration for golang packages with hot reload.
main()
nor any other part of your application has the knowledge of the package configuration.Supported file format are :
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
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)
)
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
}
MIT - see LICENSE