Back to godoc.org
gitoa.ru/go-4devs/config

Package config

v0.0.0-...-5e0325c
Latest Go to latest

The latest major version is .

Published: Aug 29, 2020 | License: MIT | Module: gitoa.ru/go-4devs/config

Index

Examples

Variables

var (
	ErrVariableNotFound = errors.New("variable not found")
	ErrInvalidValue     = errors.New("invalid value")
)

type Client

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

func New

func New(namespace, appName string, providers []Provider) *Client
Example

Code:

package main

import (
	"context"
	"fmt"
	"gitoa.ru/go-4devs/config"
	"gitoa.ru/go-4devs/config/provider/arg"
	"gitoa.ru/go-4devs/config/provider/env"
	"gitoa.ru/go-4devs/config/provider/etcd"
	"gitoa.ru/go-4devs/config/provider/json"
	"gitoa.ru/go-4devs/config/provider/vault"
	"gitoa.ru/go-4devs/config/test"
	"log"
	"os"
)

func main() {
	ctx := context.Background()
	_ = os.Setenv("FDEVS_CONFIG_LISTEN", "8080")
	_ = os.Setenv("FDEVS_CONFIG_HOST", "localhost")

	args := os.Args

	defer func() {
		os.Args = args
	}()

	os.Args = []string{
		"main.go",
		"--host=gitoa.ru",
	}

	// configure etcd client
	etcdClient, err := test.NewEtcd(ctx)
	if err != nil {
		log.Fatal(err)
	}

	// configure vault client
	vaultClient, err := test.NewVault()
	if err != nil {
		log.Fatal(err)
	}

	// read json config
	jsonConfig := test.ReadFile("config.json")

	providers := []config.Provider{
		arg.New(),
		env.New(),
		etcd.NewProvider(etcdClient),
		vault.NewSecretKV2(vaultClient),
		json.New(jsonConfig),
	}
	config := config.New(test.Namespace, test.AppName, providers)

	dsn, err := config.Value(ctx, "example:dsn")
	if err != nil {
		log.Fatal(err)
	}

	port, err := config.Value(ctx, "listen")
	if err != nil {
		log.Fatal(err)
	}

	enabled, err := config.Value(ctx, "maintain")
	if err != nil {
		log.Fatal(err)
	}

	title, err := config.Value(ctx, "app.name.title")
	if err != nil {
		log.Fatal(err)
	}

	cfgValue, err := config.Value(ctx, "cfg")
	if err != nil {
		log.Fatal(err)
	}

	hostValue, err := config.Value(ctx, "host")
	if err != nil {
		log.Fatal(err)
	}

	cfg := test.Config{}
	_ = cfgValue.Unmarshal(&cfg)

	fmt.Printf("dsn from vault: %s\n", dsn.String())
	fmt.Printf("listen from env: %d\n", port.Int())
	fmt.Printf("maintain from etcd: %v\n", enabled.Bool())
	fmt.Printf("title from json: %v\n", title.String())
	fmt.Printf("struct from json: %+v\n", cfg)
	fmt.Printf("replace env host by args: %v\n", hostValue.String())
}
dsn from vault: pgsql://user@pass:127.0.0.1:5432
listen from env: 8080
maintain from etcd: true
title from json: config title
struct from json: {Duration:21m0s Enabled:true}
replace env host by args: gitoa.ru

func (*Client) Value

func (c *Client) Value(ctx context.Context, name string) (Value, error)

func (*Client) Variable

func (c *Client) Variable(ctx context.Context, name string) (Variable, error)

type Key

type Key struct {
	Name      string
	AppName   string
	Namespace string
}

func (Key) String

func (k Key) String() string

type KeyFactory

type KeyFactory func(ctx context.Context, key Key) string

type ParseValue

type ParseValue interface {
	ParseString() (string, error)
	ParseInt() (int, error)
	ParseInt64() (int64, error)
	ParseUint() (uint, error)
	ParseUint64() (uint64, error)
	ParseFloat64() (float64, error)
	ParseBool() (bool, error)
	ParseDuration() (time.Duration, error)
	ParseTime() (time.Time, error)
}

type Provider

type Provider interface {
	Read(ctx context.Context, key Key) (Variable, error)
}

type ReadValue

type ReadValue interface {
	String() string
	Int() int
	Int64() int64
	Uint() uint
	Uint64() uint64
	Float64() float64
	Bool() bool
	Duration() time.Duration
	Time() time.Time
}

type UnmarshalValue

type UnmarshalValue interface {
	Unmarshal(val interface{}) error
}

type Value

type Value interface {
	ReadValue
	ParseValue
	UnmarshalValue
}

type Variable

type Variable struct {
	Name     string
	Provider string
	Value    Value
}

func (Variable) IsEquals

func (v Variable) IsEquals(n Variable) bool

type WatchCallback

type WatchCallback func(ctx context.Context, old, new Variable)

type WatchClient

type WatchClient struct {
	*Client
	// contains filtered or unexported fields
}

func NewWatch

func NewWatch(namespace, appName string, providers []Provider) *WatchClient
Example

Code:

package main

import (
	"context"
	"fmt"
	"gitoa.ru/go-4devs/config"
	"gitoa.ru/go-4devs/config/provider/env"
	"gitoa.ru/go-4devs/config/provider/etcd"
	"gitoa.ru/go-4devs/config/provider/watcher"
	"gitoa.ru/go-4devs/config/provider/yaml"
	"gitoa.ru/go-4devs/config/test"
	"log"
	"os"
	"sync"
	"time"
)

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	// configure etcd client
	etcdClient, err := test.NewEtcd(ctx)
	if err != nil {
		log.Fatal(err)
	}

	_ = os.Setenv("FDEVS_CONFIG_EXAMPLE_ENABLE", "true")

	_, err = etcdClient.KV.Put(ctx, "fdevs/config/example_db_dsn", "pgsql://user@pass:127.0.0.1:5432")
	if err != nil {
		log.Fatal(err)
	}

	defer func() {
		cancel()

		if _, err = etcdClient.KV.Delete(context.Background(), "fdevs/config/example_db_dsn"); err != nil {
			log.Fatal(err)
		}
	}()

	providers := []config.Provider{
		watcher.New(time.Microsecond, env.New()),
		watcher.New(time.Microsecond, yaml.NewFile("test/fixture/config.yaml")),
		etcd.NewProvider(etcdClient),
	}
	watcher := config.NewWatch(test.Namespace, test.AppName, providers)
	wg := sync.WaitGroup{}
	wg.Add(2)

	err = watcher.Watch(ctx, "example_enable", func(ctx context.Context, old, new config.Variable) {
		fmt.Println("update ", old.Provider, " variable:", old.Name, ", old: ", old.Value.Bool(), " new:", new.Value.Bool())
		wg.Done()
	})
	if err != nil {
		log.Fatal(err)
	}

	_ = os.Setenv("FDEVS_CONFIG_EXAMPLE_ENABLE", "false")

	err = watcher.Watch(ctx, "example_db_dsn", func(ctx context.Context, old, new config.Variable) {
		fmt.Println("update ", old.Provider, " variable:", old.Name, ", old: ", old.Value.String(), " new:", new.Value.String())
		wg.Done()
	})
	if err != nil {
		log.Fatal(err)
	}

	time.AfterFunc(time.Second, func() {
		_, err = etcdClient.KV.Put(ctx, "fdevs/config/example_db_dsn", "mysql://localhost:5432")
		if err != nil {
			log.Fatal(err)
		}
	})

	wg.Wait()

}
update  env  variable: FDEVS_CONFIG_EXAMPLE_ENABLE , old:  true  new: false
update  etcd  variable: fdevs/config/example_db_dsn , old:  pgsql://user@pass:127.0.0.1:5432  new: mysql://localhost:5432

func (*WatchClient) Watch

func (wc *WatchClient) Watch(ctx context.Context, name string, callback WatchCallback) error

type WatchProvider

type WatchProvider interface {
	Watch(ctx context.Context, key Key, callback WatchCallback) error
}

Package Files

  • client.go
  • error.go
  • key.go
  • provider.go
  • value.go
  • variable.go
Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier