Skip to content
This repository has been archived by the owner on Jun 13, 2019. It is now read-only.
/ confl Public archive

manage configurations with etcd and vault or file!

License

Notifications You must be signed in to change notification settings

alibaba-archive/confl

Repository files navigation

Confl

Load or reload configuration!

Build Status Coverage Status License GoDoc

Features

  • Simple API for use
  • Used as a library
  • Care about updates of configuration
  • Support Auto-Reload

Getting Started

Install

Install confl

go get -u -v github.com/teambition/confl

Usage

Watch a configuration file

package main

import (
	"fmt"

	"github.com/teambition/confl"
	"github.com/teambition/confl/examples/config"
	"gopkg.in/yaml.v2"
)

func main() {
	watcher, err := confl.NewFileWatcher(&config.Config{}, "./default.yaml", yaml.Unmarshal)
	if err != nil {
		panic(err)
	}
	defer watcher.Close()

	watcher.OnError(func(err error) {
		fmt.Println("your error handler start")
		fmt.Println(err)
	})

	// add hook for update events
	// perhaps you need reload something that depends the configuration
	watcher.AddHook(func(oc, nc interface{}) {
		ocfg := oc.(config.Config)
		ncfg := nc.(config.Config)
		// use cfg
		fmt.Printf("old config: %#v\n", ocfg)
		fmt.Printf("new config: %#v\n", ncfg)
	})

	// get configuration from watcher
	cfg := watcher.Config().(config.Config)
	// use cfg
	fmt.Printf("load config: %#v\n", cfg)

	// start watch
	// it is a blocking method choose run with `go` by situation
	watcher.Watch()
}

More example