conf

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2025 License: BSD-3-Clause Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BindPFlags

func BindPFlags()

func GetServiceConf added in v0.1.2

func GetServiceConf[T any]() T

func Load

func Load[T any](filepath string, cfg *T, onChange func(e fsnotify.Event)) error

func SetApp added in v0.1.2

func SetApp[T any](appCfg *App[T])

Types

type App added in v0.1.1

type App[T any] struct {
	Basic   `mapstructure:",squash"`
	Service T `mapstructure:"service"` // 应用自己的其他配置
}

App 应用配置设计

1.支持热加载
2.多种文件类型
3.配置值格式校验
4.配置值默认值
5.配置特殊类型解析
6.支持配置值脱敏输出
7.支持多配置文件
优先级: (相同key)

	1.主配置文件优先级最高
	2.configs 数组索引越小优先级越高

func LoadApp added in v0.1.1

func LoadApp[T any](filepath string, onChange func(e fsnotify.Event)) (*App[T], error)

LoadApp 加载配置文件 配置文件有变化时,会自动全部重新加载配置文件 优先级: (相同key)

1.主配置文件优先级最高
2.configs 数组索引越小优先级越高

type Basic

type Basic struct {
	Name    string       `mapstructure:"name" validate:"required"`  // 服务名称
	Version string       `mapstructure:"version" validate:"semver"` // 服务版本
	Env     enum.EnvType `mapstructure:"env" validate:"oneof=dev test prod"`
	// 和主配置文件的在同一个目录可以只写文件名加后缀
	Configs []string `mapstructure:"configs"` // 其他配置文件的路径
	// 注册中心的地址
	Registry Transport `mapstructure:"registry"`
	Server   struct {
		Http Transport `mapstructure:"http"`
		Rpc  Transport `mapstructure:"rpc"` // rpc 端口号没有指定,就是http端口号+1000
	} `mapstructure:"server"`
	Security   security.Config            `mapstructure:"security"`
	Logger     logger.Config              `mapstructure:"logger"`
	DB         map[string]db.Config       `mapstructure:"db"` // 支持多数据源 default key 必须存在
	LocalCache cache.LocalCacheConf       `mapstructure:"localCache" yaml:"localCache"`
	Redis      map[string]cache.RedisConf `mapstructure:"redis"` // 支持多数据源 default key 必须存在
	Kafka      kafka.Config               `mapstructure:"kafka"`
}

Basic 服务必要的配置文件

func GetBasicConf added in v0.1.2

func GetBasicConf() Basic

type Transport

type Transport struct {
	Addr    string         `mapstructure:"addr"`                                      // 监听地址 0.0.0.0:80
	Timeout types.Duration `mapstructure:"timeout" validate:"duration"  default:"5s"` // 超时时间 1s
}

Jump to

Keyboard shortcuts

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