You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.5 KiB
Go

package config
import (
"fmt"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/file"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/zrpc"
)
type (
KafkaConf struct {
Addr []string // 连接地址
Topic string
}
KafkaProducer struct {
UserCoinNotify KafkaConf // 用户金币变动通知
RankUpdate KafkaConf // 排行榜更新通知队列
RewardPool KafkaConf // 奖池通知
}
// Config 配置
Config struct {
zrpc.RpcServerConf
Log logx.LogConf
DB struct {
DataSource string
}
// Kafka 消息队列配置
Kafka KafkaProducer
}
)
var k = koanf.New(".")
func MustLoad(filepath string, v interface{}) {
f := file.Provider(filepath)
err := f.Watch(func(event interface{}, err error) {
if err != nil {
fmt.Printf("配置文件热重载失败: %v\n", err)
return
}
k = koanf.New(".")
err = loadAndUnmarshal(f, v)
if err != nil {
fmt.Printf("配置文件热重载失败: %v\n", err)
return
}
fmt.Printf("重载配置文件: %+v\n", v)
})
if err != nil {
panic(err)
}
mustLoadAndUnmarshal(f, v)
}
func loadAndUnmarshal(f *file.File, v interface{}) (err error) {
err = k.Load(f, yaml.Parser())
if err != nil {
return
}
err = k.UnmarshalWithConf("", v, koanf.UnmarshalConf{Tag: "cfg"})
if err != nil {
return
}
return
}
func mustLoadAndUnmarshal(f *file.File, v interface{}) {
if err := loadAndUnmarshal(f, v); err != nil {
panic(err)
}
}