conf

package
v0.0.0-...-a69402d Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2023 License: MIT Imports: 8 Imported by: 0

README

conf

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

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

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

框架还会自动监听 sniper.toml 内容变更,发现变更会自动热加载。

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

示例

import "github.com/busyfree/leaf-go/util/conf"

b := conf.GetBool("IS_SHUTTING_DOWN")

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

View Source
var (
	BinBuildVersion = ""
	BinBuildCommit  = ""
	BinBuildDate    = ""
	BinAppName      = ""
)
View Source
var (
	// Hostname 主机名
	Hostname = "localhost"
	// AppID 获取 APP_ID
	AppID = "localapp"
	// 机器ID
	MachineID uint16 = 0
	// IsDevEnv 开发环境标志
	IsDevEnv = false
	// IsUatEnv 集成环境标志
	IsUatEnv = false
	// IsProdEnv 生产环境标志
	IsProdEnv = false
	// Env 运行环境
	Env = "dev"
	// Zone 服务区域
	Zone = "qd001"
)

Functions

func GetBool

func GetBool(key string) bool

GetBool 获取配置布尔配置

func GetConfigPath

func GetConfigPath() string

func GetDuration

func GetDuration(key string) time.Duration

GetDuration 获取时间配置

func GetFloat64

func GetFloat64(key string) float64

GetFloat64 获取浮点数配置

func GetInt

func GetInt(key string) int

GetInt 获取整数配置

func GetInt32

func GetInt32(key string) int32

GetInt32 获取 int32 配置

func GetInt64

func GetInt64(key string) int64

GetInt64 获取 int64 配置

func GetIntSlice

func GetIntSlice(key string) (s []int64, err error)

GetIntSlice 获取数字列表 1,2,3 => []int64{1,2,3}

func GetProjectDBName

func GetProjectDBName(key1, key2 string) string

func GetStrMapStr

func GetStrMapStr(key string) map[string]string

func GetString

func GetString(key string) string

GetString 获取字符串配置

func GetStringSlice

func GetStringSlice(key string) (s []string)

GetStringSlice 获取字符串列表 a,b,c => []string{"a", "b", "c"}

func GetTime

func GetTime(key string, args ...string) time.Time

GetTime 查询时间配置 默认时间格式为 "2006-01-02 15:04:05",conf.GetTime("FOO_BEGIN") 如果需要指定时间格式,则可以多传一个参数,conf.GetString("FOO_BEGIN", "2006")

配置不存在或时间格式错误返回**空时间对象** 使用本地时区

func IsSet

func IsSet(key string) bool

func OnConfigChange

func OnConfigChange(run func())

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

func Set

func Set(key string, value string)

Set 设置配置,仅用于测试

func WatchConfig

func WatchConfig()

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

Types

This section is empty.

Jump to

Keyboard shortcuts

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