config

package module
v0.0.0-...-2e12048 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2020 License: MIT Imports: 4 Imported by: 6

README

config-client

介绍

配置中心 go客户端

软件架构

通过API加载配置,写入到viper

用法
import "gitee.com/wxlao/config-client"

func init() {
	config.LoadFile(".env")

	configServerURL := "http://localhost:7777/api/fetch_all_configs?env=test&appName=your-app&version=0.0.0"
	config.LoadRemote(configServerURL)

	cfgs := map[string]interface{}{
		"a": 1,
		"b": "2",
		"c": []string{"1", "2", "3"},
		"d": []int{1, 2, 3},
	}
	config.SetDefaults(cfgs)
}

func main() {
	a := viper.GetInt("a")
	fmt.Println(a)

	b := config.DefaultClient.Viper().GetString("b")
	fmt.Println(b)

	c := viper.GetStringSlice("c")
	fmt.Println(c)

	d := config.DefaultClient.Viper().GetIntSlice("d")
	fmt.Println(d)

}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultClient = New()

DefaultClient 默认客户端

Functions

func LoadFile

func LoadFile(name string) error

LoadFile 加载文件配置 搜索路径 . ./config ./configs

func LoadRemote

func LoadRemote(url string) error

LoadRemote 加载服务端配置

func SetDefaults

func SetDefaults(data map[string]interface{}) error

SetDefaults 设置默认值

Types

type Client

type Client interface {
	// 获取viper
	Viper() *viper.Viper
	// 加载远端配置
	LoadRemote(url string) error
	// 加载文件配置
	LoadFile(name string) error
	// 默认值
	SetDefaults(data map[string]interface{}) error
}

Client 客户端

func New

func New(opts ...Option) Client

New 新建实例

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option 配置

func WithAutoEnv

func WithAutoEnv(autoEnv bool) Option

WithAutoEnv 实时加载环境变量 默认true

func WithViper

func WithViper(viper *viper.Viper) Option

WithViper viper实例 默认viper

Jump to

Keyboard shortcuts

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