autoconfig

package module
v0.0.0-...-0dfd486 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2015 License: MIT Imports: 7 Imported by: 0

README

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

Documentation

Overview

Package autoconfig allows packages to be configured autonomously and reconfigured automatically.

Each package has its own configuration section in a 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 INI (using https://github.com/go-ini/ini) and YAML (using https://gopkg.in/yaml.v2).

Usage - YAML

Init :

autoconfig.Load(yaml.New(cfgfile))
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
}

var (
	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
	}
}

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

Usage - INI

Init :

autoconfig.Load(ini.New(cfgfile))
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).

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoLoader = errors.New("No loader was defined")
)

Functions

func Get

func Get(name string) (interface{}, bool)

Get returns the configuration for a section

func Load

func Load(l Loader) error

Load defines the loader for the default config, and loads the config file.

func MustGet

func MustGet(name string) interface{}

MustGet returns the configuration for the specified section from the default configuration. If the section does not exist, something will panic.

func Reconfigure

func Reconfigure(name string, r Reconfigurable) bool

Reconfigure registers an instance to the default config. The config section must have been registered before using Register

func New() *PkgClass {
	c := &PkgClass{}
	config.Reconfigure("section_name", c)
	return c
}

func Register

func Register(name string, s interface{}) bool

Register registers a config structure for a config file section. The values passed will be used as defaults in the future. Defaults will be remembered : if a variable is defined, and then unset, it will be reset to the default value. If s implements UpdateableConfig, s.Changed() will be called when the config is reloaded and has changed.

var (
	_ = config.Register("section_name", &PkgConfig{Value: "default"})
)

func Reload

func Reload() error

Reload reloads the config file for the default config

func ReloadOn

func ReloadOn(signals ...os.Signal)

ReloadOn defines signal to monitor. On reception of a signal, the default config will be reloaded.

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config defines a config

func New

func New(l Loader) *Config

New defines a config, based on a loader.

func (*Config) Get

func (c *Config) Get(name string) (interface{}, bool)

Get returns the configuration for a section

func (*Config) Load

func (c *Config) Load() error

Load loads the config by calling the Load() function of the loader.

func (*Config) MustGet

func (c *Config) MustGet(name string) interface{}

MustGet returns the configuration for the specified section. If the section does not exist, something will panic.

func (*Config) Reconfigure

func (c *Config) Reconfigure(name string, r Reconfigurable) bool

Reconfigure registers an instance. The config section must have been registered before using Register r.Reconfigure() will be called when config is reloaded and has changed. If config has been previously loaded, r.Reconfigure() will be called immediatly.

func (*Config) Register

func (c *Config) Register(name string, s interface{}) bool

Register registers a config structure for a config file section. The values passed will be used as defaults in the future. Defaults will be remembered : if a variable is defined, and then unset, it will be reset to the default value. If s implements UpdateableConfig, s.Changed() will be called when the config is reloaded and has changed. If config has been previously loaded, s.Changed() will be called immediatly.

func (*Config) Reload

func (c *Config) Reload() error

Reload reloads the config file

func (*Config) ReloadOn

func (c *Config) ReloadOn(signals ...os.Signal)

ReloadOn defines signal to monitor. On reception of a signal, the config will be reloaded.

type Loader

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

Loader defines the interface a config file loader will need to implement.

type Reconfigurable

type Reconfigurable interface {
	Reconfigure(interface{})
}

Reconfigurable defines the interface updateable instances need to implement. Each time the config is reloaded and the corresponding config section has changed, the Reconfigure function will be called for all instances.

type UpdatableConfig

type UpdatableConfig interface {
	Changed()
}

UpdatableConfig defines the interface updateable config need to implement. Each time the config is reloaded and the corresponding config section has changed, the Changed function will be called.

Directories

Path Synopsis
Package ini defines a loader for ini config files autoconfig.Load(ini.New(filename))
Package ini defines a loader for ini config files autoconfig.Load(ini.New(filename))
Package yaml defines a loader for yaml config files autoconfig.Load(yaml.New(filename))
Package yaml defines a loader for yaml config files autoconfig.Load(yaml.New(filename))

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL