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"
|
2025-06-06 00:09:10 +08:00
|
|
|
"game/common/constant"
|
2025-05-25 20:34:08 +08:00
|
|
|
"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
|
2025-06-09 23:52:18 +08:00
|
|
|
var Cfg *config.Common
|
2025-06-15 20:22:54 +08:00
|
|
|
var GateCfg = &GateConfig{}
|
2025-05-25 20:34:08 +08:00
|
|
|
|
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()
|
2025-06-06 00:09:10 +08:00
|
|
|
rdb, err := db.InitRedis(Command.RedisPassword, Command.RedisHost, Command.RedisPort, constant.Redis0Config)
|
2025-05-25 20:34:08 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() { _ = rdb.Close() }()
|
2025-06-15 20:22:54 +08:00
|
|
|
Cfg, err = config.LoadCommonConfig(rdb, GitCommit, GitBranch, BuildDate)
|
2025-05-25 20:34:08 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2025-06-15 20:22:54 +08:00
|
|
|
if err = config.LoadGameConfig(rdb, gateKey, Cfg); err != nil {
|
2025-05-25 22:39:54 +08:00
|
|
|
log.DebugF("load config:empty etcd key")
|
2025-06-09 23:52:18 +08:00
|
|
|
GateCfg = &GateConfig{
|
2025-05-29 17:34:47 +08:00
|
|
|
Address: []string{gateAddress1, gateAddress2},
|
2025-05-25 22:39:54 +08:00
|
|
|
}
|
2025-06-09 23:52:18 +08:00
|
|
|
if bs, err := json.Marshal(GateCfg); err == nil {
|
2025-05-25 22:39:54 +08:00
|
|
|
err = rdb.Set(context.Background(), gateKey, string(bs), 0).Err()
|
|
|
|
}
|
2025-06-09 23:52:18 +08:00
|
|
|
} else {
|
|
|
|
_ = json.Unmarshal([]byte(Cfg.GameJson), GateCfg)
|
2025-05-25 22:39:54 +08:00
|
|
|
}
|
2025-05-25 20:34:08 +08:00
|
|
|
log.DebugF("load common config success")
|
2025-06-15 20:29:05 +08:00
|
|
|
_ = Cfg
|
2025-05-25 20:34:08 +08:00
|
|
|
}
|