config

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2022 License: MIT Imports: 8 Imported by: 5

README

config

配置接入组件

目标

提供统一的配置接入接口, 所有项目统一格式和入口,降低沟通和理解成本

接口说明

Configer 定义了配置的主要接口,GetSettingXXX:获取指定section下的key的值 Section 定义了一个配置的Section,通常作为一个逻辑上的模块配置,避免一些混淆。 每个模块维护自己section中的配置,避免模块个性化配置被共享。 GetXXXXMust: 获取指定key的value,如果获取失败,则以默认值填充。

实现

目前支持类ini配置,简单的toml配置,所有配置建议拆分成kv模式,方便配置管理和服务发现的接入。 复杂格式的配置,会增加管理和扩展成本。

举例

test.conf

[logger]
type = stdout
level = DEBUG

[read-mysql]
host = 127.0.0.1
port = 3306
db = test
pool_size = 64

Documentation

Overview

Package config ...

Package config ...

Package config ...

Package config ...

Package config ...

Package config ...

Index

Constants

View Source
const (
	// ConfFormatTypeIni ini
	ConfFormatTypeIni confFormatType = iota
	// ConfFormatTypeToml toml
	ConfFormatTypeToml
	// ConfFormatTypeEtcd etcd
	ConfFormatTypeEtcd
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Configer

type Configer interface {
	// load config content
	Load() error

	// Return when config changed
	LastModify() (time.Time, error)

	GetAllSections() map[string]Sectioner
	GetSection(sec string) (Sectioner, error)
	GetIntSetting(sec, key string) (val int64, err error)
	GetBoolSetting(sec, key string) (val bool, err error)
	GetFloatSetting(sec, key string) (val float64, err error)
	GetSetting(sec, key string) (val string, err error)
}

Configer ...

func New

func New(confPath string) (Configer, error)

New return default ini-configer

func NewConfigWithFormatType

func NewConfigWithFormatType(ft confFormatType, confPath string) (Configer, error)

NewConfigWithFormatType new configer according to conf-type

type Sectioner

type Sectioner interface {
	GetInt(key string) (val int64, err error)
	GetBool(key string) (val bool, err error)
	GetFloat(key string) (val float64, err error)
	GetString(key string) (val string, err error)

	// Return defaultVal when key missed
	GetIntMust(key string, defaultVal int64) int64
	GetBoolMust(key string, defaultVal bool) bool
	GetFloatMust(key string, defaultVal float64) float64
	GetStringMust(key string, defaultVal string) string
}

Sectioner contains key-value of each module

type Watcher

type Watcher interface {
	Watch() <-chan []byte
}

Watcher should be implemented by service-discovery such as etcdcli...

Jump to

Keyboard shortcuts

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