game/common/config/loadConfig.go

159 lines
4.5 KiB
Go
Raw Normal View History

2025-05-25 20:34:08 +08:00
package config
import (
"context"
"encoding/json"
2025-05-25 22:39:54 +08:00
"errors"
2025-05-25 20:34:08 +08:00
"flag"
"fmt"
"game/common/testHelper"
2025-05-25 20:34:08 +08:00
"github.com/fox/fox/log"
"github.com/go-redis/redis/v8"
)
const (
2025-05-25 22:39:54 +08:00
//ModeDev = "dev" // 开发服
2025-05-29 00:17:18 +08:00
etcdKey = "etcd_config"
etcdAddress = "114.132.124.145:2379"
natsKey = "nats_config"
natsAddress = "nats://114.132.124.145:4222"
redisKey = "redis_config"
redisAddress = testHelper.Host
redisPort = testHelper.RedisPort
redisPassword = testHelper.RedisPassword
mysqlKey = "mysql_config"
mysqlAddress = "114.132.124.145"
mysqlPort = "3306"
mysqlUser = "game"
mysqlPasswd = "fox379@@zyxi"
mysqlDBName = "game"
mysqlLogDBName = "game_log"
2025-05-25 20:34:08 +08:00
)
func LoadSpecialConfig[T any](rd *redis.Client, specialKey string, comm *Common[T]) error {
s, err := rd.Get(context.Background(), specialKey).Result()
2025-05-25 22:39:54 +08:00
if err != nil && !errors.Is(err, redis.Nil) {
2025-05-25 20:34:08 +08:00
log.FatalF("init config:%v", err)
return err
}
if s == "" {
return fmt.Errorf("config:%s not found from redis", specialKey)
}
if err = json.Unmarshal([]byte(s), comm.Special); err != nil {
log.FatalF("init special config:%v", err)
return err
}
return nil
}
type resultT[T any] struct {
Value T
Err error
}
func LoadCommonConfig[T any](rd *redis.Client, GitCommit, GitBranch, BuildDate string) (*Common[T], error) {
2025-05-25 20:34:08 +08:00
var ret resultT[T]
var comm Common[T]
comm.Special = &ret.Value
comm.GitCommit = GitCommit
comm.GitBranch = GitBranch
comm.BuildDate = BuildDate
// 初始化etcd
2025-05-25 20:34:08 +08:00
s, err := rd.Get(context.Background(), etcdKey).Result()
2025-05-25 22:39:54 +08:00
if err != nil && !errors.Is(err, redis.Nil) {
2025-05-25 20:34:08 +08:00
log.FatalF("init config:%v", err)
return nil, err
}
if s == "" {
log.DebugF("load config:empty etcd key")
comm.Etcd = Etcd{
Address: []string{etcdAddress},
Username: "",
Password: "",
}
if bs, err := json.Marshal(&comm.Etcd); err == nil {
err = rd.Set(context.Background(), etcdKey, string(bs), 0).Err()
}
} else {
err = json.Unmarshal([]byte(s), &comm.Etcd)
}
// 初始化nats
2025-05-25 20:34:08 +08:00
s, err = rd.Get(context.Background(), natsKey).Result()
2025-05-25 22:39:54 +08:00
if err != nil && !errors.Is(err, redis.Nil) {
2025-05-25 20:34:08 +08:00
log.FatalF("init config:%v", err)
return nil, err
}
if s == "" {
log.DebugF("load config:empty nats key")
comm.Nats = Nats{Address: []string{natsAddress}}
if bs, err := json.Marshal(&comm.Nats); err == nil {
err = rd.Set(context.Background(), natsKey, string(bs), 0).Err()
}
} else {
err = json.Unmarshal([]byte(s), &comm.Nats)
}
// 初始化redis
s, err = rd.Get(context.Background(), redisKey).Result()
if err != nil && !errors.Is(err, redis.Nil) {
log.FatalF("init config:%v", err)
return nil, err
}
if s == "" {
log.DebugF("load config:empty redis key")
comm.Redis = Redis{Host: redisAddress, Port: redisPort, Password: redisPassword}
if bs, err := json.Marshal(&comm.Redis); err == nil {
err = rd.Set(context.Background(), redisKey, string(bs), 0).Err()
}
} else {
err = json.Unmarshal([]byte(s), &comm.Redis)
}
2025-05-29 00:17:18 +08:00
// 初始化mysql
s, err = rd.Get(context.Background(), mysqlKey).Result()
if err != nil && !errors.Is(err, redis.Nil) {
log.FatalF("init config:%v", err)
return nil, err
}
if s == "" {
log.DebugF("load config:empty mysql key")
comm.Mysql = Mysql{Host: mysqlAddress, Port: mysqlPort, Password: mysqlPasswd, Username: mysqlUser, DbName: mysqlDBName}
if bs, err := json.Marshal(&comm.Redis); err == nil {
err = rd.Set(context.Background(), mysqlKey, string(bs), 0).Err()
}
} else {
err = json.Unmarshal([]byte(s), &comm.Mysql)
}
comm.MysqlLog = comm.Mysql
comm.MysqlLog.DbName = mysqlLogDBName
2025-05-25 20:34:08 +08:00
return &comm, nil
}
/*
所有配置信息都存放在redis中程序启动会给配置信息的redis地址
连上后从该redis中读取所有其它的配置信息如mysqletcdnats以及玩法相关的如房间配置等信息
*/
2025-05-25 20:34:08 +08:00
type Command struct {
RedisHost string
RedisPort string
RedisPassword string
ServiceId int // 起始服务id
ServiceNum int // 服务数量
2025-05-25 20:34:08 +08:00
VMod string // 区分测试服,开发服
}
func ParseCommand() *Command {
var Cmd Command
flag.StringVar(&Cmd.RedisHost, "redis_host", "114.132.124.145", "redis host")
flag.StringVar(&Cmd.RedisPort, "redis_port", "6379", "redis port")
flag.StringVar(&Cmd.RedisPassword, "redis_password", "fox379@@zyxi", "redis password")
flag.IntVar(&Cmd.ServiceId, "sid", 0, "service id")
flag.IntVar(&Cmd.ServiceNum, "num", 0, "service num")
flag.StringVar(&Cmd.VMod, "v_mod", "dev", "dev|release|test|prod")
flag.Parse()
return &Cmd
}