Coder Social home page Coder Social logo

cfg's Introduction

cfg: Reloadable config package for golang

Features

  1. Use golang struct instead of map for config.
  2. Versioned and Atomic reloadable configuration
  3. Customizable decoders(json out of box)
  4. Customizable config source(reloadable file source out of box)
  5. Hooks in config lifecycle, and can be used to customize behavior of cfg package:
    1. PreDecoder: called before new config decoding from source. It can be used to set default value.
    2. PostDecoder: called after new config decoding from source. It can be used to validate new config.
    3. PostSwap: called after new config taked effect(implemented by atomic swap pointers). It can be used to notify that new config is reloaded.
  6. Simple logger interface to log events.

How to use

import (
    validator "github.com/go-playground/validator/v10"
    "github.com/gokits/cfg"
    "github.com/gokits/cfg/source/file"
)
//...

// 1. define config struct
// 2. define validator(optional)
type Config struct {
    MaxRetry               int  `validate:"min=0,max=10"`
    LogPath                string
}

var (
    gvalidator *validator.Validate
    filesource *file.File
    meta       *cfg.ConfigMeta
)
//...

// define PostDecode hook to validate new config. New config reload will be canceled if `error != nil`.
// `oldptr` will hold pointer to current version config struct
// `c` will hold pointer to new config struct
func (c *Config) PostDecode(oldptr interface{}) error {
    return gvalidator.Struct(c)
}

//...

func main() { 
    gvalidator = validator.New()
    if filesource, err = file.NewFileSource("./tmp.json"); err != nil {
        return
    }
    defer filesource.Close()
    meta = cfg.NewConfigMeta(Config{}, filesource)
    go meta.Run()
    if err = meta.WaitSynced(); err != nil {
        return
    }
    fmt.Printf("%+v\n", meta.Get().(*Config))
}

Roadmap

  1. [done] reloadable file source
  2. [done] custimizable hooks
  3. [todo] export config version
  4. [todo] frozen package api and release v1
  5. [todo] implement other config source(etcd, consul...)

Status

This package has been used in production environment running on our kubernetes clusters. It works well with configmap of kubernetes and files.

cfg's People

Contributors

honghuiyue avatar yuehonghui avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.