125 lines
3.3 KiB
Go
125 lines
3.3 KiB
Go
package config
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"game/common/testHelper"
|
|
"github.com/fox/fox/log"
|
|
"github.com/go-redis/redis/v8"
|
|
)
|
|
|
|
const (
|
|
//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
|
|
)
|
|
|
|
func LoadSpecialConfig[T any](rd *redis.Client, specialKey string, comm *Common[T]) error {
|
|
s, err := rd.Get(context.Background(), specialKey).Result()
|
|
if err != nil && !errors.Is(err, redis.Nil) {
|
|
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
|
|
s, err := rd.Get(context.Background(), etcdKey).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 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
|
|
s, err = rd.Get(context.Background(), natsKey).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 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)
|
|
}
|
|
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
|
|
}
|