conf

package
v0.0.0-...-e5591e2 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2023 License: MIT Imports: 6 Imported by: 0

README

conf

默认从 sniper.toml 加载配置。虽然 sniper.toml 支持复杂的数据结构, 但框架要求只能设置 k-v 型配置。目的是为了跟环境变量相兼容。

sniper.toml 中的所有配置项都可以使用环境变量覆写。

如果配置文件不在项目根目录,则可以通过环境变量CONF_PATH指定。

框架还会自动监听CONF_PATH目录下所有 toml 内容变更,发现变更会自动加载。

最后,配置名跟环境变量一样,不区分大小写字母。

示例

import "github.com/go-kiss/sniper/pkg/conf"

a := conf.Get("LOG_LEVEL")

b := conf.File("foo").GetInt32("WORKER_NUM")

Sniper 的 memdb/sqldb 等组件依赖 conf 组件。如果不想通过文件的方式加载配置, 则可以覆盖conf.Get方法实现新的配置加载逻辑。

Documentation

Overview

Package conf 提供最基础的配置加载功能

Index

Constants

This section is empty.

Variables

View Source
var (
	// Host 主机名
	Host = "localhost"
	// App 服务标识
	App = "localapp"
	// Env 运行环境
	Env = "dev"
	// Zone 服务区域
	Zone = "sh001"
)
View Source
var Get func(string) string

Get 查询配置/环境变量

Functions

func GetBool

func GetBool(key string) bool

func GetDuration

func GetDuration(key string) time.Duration

func GetFloat64

func GetFloat64(key string) float64

func GetInt

func GetInt(key string) int

func GetInt32

func GetInt32(key string) int32

func GetInt64

func GetInt64(key string) int64

func GetIntSlice

func GetIntSlice(key string) []int

func GetSizeInBytes

func GetSizeInBytes(key string) uint

func GetString

func GetString(key string) string

func GetStringMap

func GetStringMap(key string) map[string]interface{}

func GetStringMapString

func GetStringMapString(key string) map[string]string

func GetStringMapStringSlice

func GetStringMapStringSlice(key string) map[string][]string

func GetStringSlice

func GetStringSlice(key string) []string

func GetTime

func GetTime(key string) time.Time

func GetUint

func GetUint(key string) uint

func GetUint32

func GetUint32(key string) uint32

func GetUint64

func GetUint64(key string) uint64

func OnConfigChange

func OnConfigChange(run func())

OnConfigChange 注册配置文件变更回调 需要在 WatchConfig 之前调用

func Set

func Set(key string, value interface{})

Set 设置配置,仅用于测试

func WatchConfig

func WatchConfig()

WatchConfig 启动配置变更监听,业务代码不要调用。

Types

type Conf

type Conf struct {
	*viper.Viper
}

func File

func File(name string) *Conf

File 根据文件名获取对应配置对象 目前仅支持 toml 文件,不用传扩展名 如果要读取 foo.toml 配置,可以 File("foo").Get("bar")

Jump to

Keyboard shortcuts

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