game/common/config/loadConfig.go

170 lines
4.9 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-06-14 16:40:15 +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"
clickhouseKey = "clickhouse_config"
clickhouseAddress = "114.132.124.145"
2025-06-15 20:06:00 +08:00
clickhousePort = "9000"
2025-06-14 16:40:15 +08:00
clickhouseUser = "game"
clickhousePasswd = "fox379@@zyxi"
clickhouseDBName = "game_log"
2025-05-25 20:34:08 +08:00
)
2025-06-09 23:52:18 +08:00
func LoadGameConfig(rd *redis.Client, specialKey string, comm *Common) error {
2025-05-25 20:34:08 +08:00
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-06-07 22:53:54 +08:00
log.ErrorF("init config:%v", err)
2025-05-25 20:34:08 +08:00
return err
}
if s == "" {
return fmt.Errorf("config:%s not found from redis", specialKey)
}
2025-06-09 23:52:18 +08:00
comm.GameJson = s
2025-05-25 20:34:08 +08:00
return nil
}
2025-06-09 23:52:18 +08:00
func LoadCommonConfig(rd *redis.Client, GitCommit, GitBranch, BuildDate string) (*Common, error) {
var comm Common
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")
2025-06-14 16:40:15 +08:00
comm.UserDb = Mysql{Host: mysqlAddress, Port: mysqlPort, Password: mysqlPasswd, Username: mysqlUser, DbName: mysqlDBName}
if bs, err := json.Marshal(&comm.UserDb); err == nil {
2025-05-29 00:17:18 +08:00
err = rd.Set(context.Background(), mysqlKey, string(bs), 0).Err()
}
} else {
2025-06-14 16:40:15 +08:00
err = json.Unmarshal([]byte(s), &comm.UserDb)
2025-05-29 00:17:18 +08:00
}
2025-06-14 16:40:15 +08:00
// 初始化clickhouse
s, err = rd.Get(context.Background(), clickhouseKey).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")
2025-06-15 20:06:00 +08:00
comm.LogDb = ClickHouse{Host: clickhouseAddress, Port: clickhousePort, Password: clickhousePasswd,
Username: clickhouseUser, DbName: clickhouseDBName}
if bs, err := json.Marshal(&comm.LogDb); err == nil {
2025-06-14 16:40:15 +08:00
err = rd.Set(context.Background(), clickhouseKey, string(bs), 0).Err()
}
} else {
2025-06-15 20:06:00 +08:00
err = json.Unmarshal([]byte(s), &comm.LogDb)
2025-06-14 16:40:15 +08:00
}
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")
2025-05-29 23:02:40 +08:00
flag.IntVar(&Cmd.ServiceNum, "num", 1, "service num")
2025-05-25 20:34:08 +08:00
flag.StringVar(&Cmd.VMod, "v_mod", "dev", "dev|release|test|prod")
flag.Parse()
return &Cmd
}