game/common/config/loadConfig.go

125 lines
3.3 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" // 开发服
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
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) (*Common[T], error) {
var ret resultT[T]
var comm Common[T]
comm.Special = &ret.Value
// 初始化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-25 20:34:08 +08:00
return &comm, nil
}
type Command struct {
RedisHost string
RedisPort string
RedisPassword string
ServiceId int
ServiceNum int
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
}