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 (
|
2025-05-29 17:34:47 +08:00
|
|
|
gateKey = "gate_config"
|
2025-05-29 23:02:40 +08:00
|
|
|
gateAddress1 = "0.0.0.0:5100"
|
|
|
|
gateAddress2 = "0.0.0.0:5101"
|
2025-05-25 22:39:54 +08:00
|
|
|
)
|
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 {
|
2025-05-29 17:34:47 +08:00
|
|
|
Address []string `json:"address"` // 网关地址
|
2025-05-25 22:39:54 +08:00
|
|
|
}
|
|
|
|
|
2025-05-30 23:15:53 +08:00
|
|
|
func InitLog() {
|
2025-05-29 23:02:40 +08:00
|
|
|
log.Open("./log/gate.log", log.DebugL)
|
2025-05-30 23:19:12 +08:00
|
|
|
log.Info("")
|
|
|
|
log.Info("")
|
|
|
|
log.Info("")
|
|
|
|
log.Info("-----init log success-----")
|
2025-05-25 20:34:08 +08:00
|
|
|
}
|
|
|
|
|
2025-05-28 20:19:11 +08:00
|
|
|
func LoadConfig(GitCommit, GitBranch, BuildDate string) {
|
2025-05-25 20:34:08 +08:00
|
|
|
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() }()
|
2025-05-28 20:19:11 +08:00
|
|
|
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{
|
2025-05-29 17:34:47 +08:00
|
|
|
Address: []string{gateAddress1, gateAddress2},
|
2025-05-25 22:39:54 +08:00
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|