game/server/gate/config/config.go

55 lines
1.2 KiB
Go
Raw Normal View History

2025-05-25 20:34:08 +08:00
package config
import (
2025-05-25 22:39:54 +08:00
"context"
"encoding/json"
2025-05-25 20:34:08 +08:00
"game/common/config"
"github.com/fox/fox/db"
"github.com/fox/fox/log"
)
2025-05-25 22:39:54 +08:00
const (
gateKey = "gate_config"
gateAddress = "114.132.124.145"
gatePort = "5100"
)
2025-05-25 20:34:08 +08:00
var Command *config.Command
var Cfg *config.Common[GateConfig]
2025-05-25 22:39:54 +08:00
type GateConfig struct {
Address string `json:"address"` // 网关地址
2025-05-25 22:39:54 +08:00
Port string `json:"port"`
}
2025-05-25 20:34:08 +08:00
func initLog() {
2025-05-29 17:21:14 +08:00
log.Open("gate.log", log.DebugL)
2025-05-25 20:34:08 +08:00
}
func LoadConfig(GitCommit, GitBranch, BuildDate string) {
2025-05-25 20:34:08 +08:00
Command = config.ParseCommand()
initLog()
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)
2025-05-25 20:34:08 +08:00
if err != nil {
log.Error(err.Error())
return
}
2025-05-25 22:39:54 +08:00
if err = config.LoadSpecialConfig[GateConfig](rdb, gateKey, Cfg); err != nil {
log.DebugF("load config:empty etcd key")
Cfg.Special = &GateConfig{
Address: gateAddress,
Port: gatePort,
}
if bs, err := json.Marshal(&Cfg.Special); err == nil {
err = rdb.Set(context.Background(), gateKey, string(bs), 0).Err()
}
}
2025-05-25 20:34:08 +08:00
log.DebugF("load common config success")
}