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.

113 lines
2.2 KiB
Go

package config
import (
"fmt"
"git.noahlan.cn/northlan/ntools-go/logger"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/file"
"github.com/zeromicro/go-zero/zrpc"
)
type (
Config struct {
// Log 日志配置
Log struct {
File logger.FileConfig
Console logger.ConsoleConfig
}
Server struct {
Debug bool
Listen string
}
// Kafka 队列配置
Kafka struct {
Danmaku Kafka // 发送弹幕
Gift Kafka // 赠送礼物
GuardBuy Kafka // TODO 购买贵族 改名
RewardPool Kafka // 奖池消息
UserCoin Kafka // 用户金币变动消息
}
Game struct {
// DefaultRooms 默认房间
DefaultRooms []struct {
ID int64 // ID
GameType string // 类型
}
AdminUserId []int64 // 管理员ID
Zhg Zhg // 指挥官
// Zhghz 指挥官海战模式
Zhghz struct {
}
// Zhgfb 指挥官副本模式
Zhgfb struct {
}
Zhgzd Zhgzd // Zhgzd 指挥官-阵地
Zhgww2 Zhgww2 // Zhgww2 指挥官-二战
Zhgmang Zhgmang // Zhgmang 指挥官-莽
}
// RPC
UserCenterRpc zrpc.RpcClientConf
}
Option func(cfg *Config)
)
var k = koanf.New(".")
var f *file.File
func MustLoad(filepath string, opts ...Option) *Config {
var cfg Config
f = file.Provider(filepath)
mustLoadAndUnmarshal(f, &cfg)
for _, opt := range opts {
opt(&cfg)
}
fmt.Printf("%+v\n", cfg)
return &cfg
}
func WithHotReload() Option {
return func(cfg *Config) {
err := f.Watch(func(event interface{}, err error) {
if err != nil {
fmt.Printf("配置文件热重载失败: %v\n", err)
return
}
k = koanf.New(".")
err = loadAndUnmarshal(f, cfg)
if err != nil {
fmt.Printf("配置文件热重载失败: %v\n", err)
return
}
fmt.Printf("重载配置文件: %+v\n", cfg)
})
if err != nil {
panic(err)
}
}
}
func loadAndUnmarshal(f *file.File, cfg *Config) (err error) {
err = k.Load(f, yaml.Parser())
if err != nil {
return
}
err = k.UnmarshalWithConf("", cfg, koanf.UnmarshalConf{Tag: "c"})
if err != nil {
return
}
return
}
func mustLoadAndUnmarshal(f *file.File, cfg *Config) {
if err := loadAndUnmarshal(f, cfg); err != nil {
panic(err)
}
}