game/server/gate/config/config.go
2025-05-30 23:19:12 +08:00

56 lines
1.3 KiB
Go

package config
import (
"context"
"encoding/json"
"game/common/config"
"github.com/fox/fox/db"
"github.com/fox/fox/log"
)
const (
gateKey = "gate_config"
gateAddress1 = "0.0.0.0:5100"
gateAddress2 = "0.0.0.0:5101"
)
var Command *config.Command
var Cfg *config.Common[GateConfig]
type GateConfig struct {
Address []string `json:"address"` // 网关地址
}
func InitLog() {
log.Open("./log/gate.log", log.DebugL)
log.Info("")
log.Info("")
log.Info("")
log.Info("-----init log success-----")
}
func LoadConfig(GitCommit, GitBranch, BuildDate string) {
Command = config.ParseCommand()
rdb, err := db.InitRedis(Command.RedisPassword, Command.RedisHost, Command.RedisPort, 0)
if err != nil {
log.Error(err.Error())
return
}
defer func() { _ = rdb.Close() }()
Cfg, err = config.LoadCommonConfig[GateConfig](rdb, GitCommit, GitBranch, BuildDate)
if err != nil {
log.Error(err.Error())
return
}
if err = config.LoadSpecialConfig[GateConfig](rdb, gateKey, Cfg); err != nil {
log.DebugF("load config:empty etcd key")
Cfg.Special = &GateConfig{
Address: []string{gateAddress1, gateAddress2},
}
if bs, err := json.Marshal(&Cfg.Special); err == nil {
err = rdb.Set(context.Background(), gateKey, string(bs), 0).Err()
}
}
log.DebugF("load common config success")
}