From 533598d0c8734e2cb25558ac82e333096bad6bbb Mon Sep 17 00:00:00 2001 From: 王家文 Date: Tue, 16 Apr 2024 15:33:49 +0800 Subject: [PATCH] refactor♻️:项目目录重构 --- common/svconfig/interface.go | 23 +++++++++++++++++++++++ configs/api.go | 38 ++++++++++++++++++++++++++++++++++++++ configs/index.go | 16 ++++++++++++++++ configs/registry.go | 1 + main.go | 9 +++++++++ util/zredis/kv.go | 10 ++++++++++ 6 files changed, 97 insertions(+), 0 deletions(-) create mode 100644 common/svconfig/interface.go create mode 100644 configs/api.go create mode 100644 configs/index.go create mode 100644 configs/registry.go diff --git a/common/svconfig/interface.go b/common/svconfig/interface.go new file mode 100644 index 0000000..628f954 --- /dev/null +++ b/common/svconfig/interface.go @@ -0,0 +1,23 @@ +package svconfig + +import "gorm.io/gorm" + +const ( + MYSQL_TABLE_TEMPLATE = "all_table_template" // 表名模板 + REDIS_CACHEP_REFIX = "apigame" // 缓存前缀 +) + +// ConfigRule 配置数据 +type ConfigRule struct { + DbMysql *gorm.DB + TableNameTemplate string + TableName string + CacheKey string + CacheTime int +} + +// IConfigRule 配置数据 +type IConfigRule interface { + // GetRule 表名模板 + GetRule(gameId string) *ConfigRule +} diff --git a/configs/api.go b/configs/api.go new file mode 100644 index 0000000..60b4479 --- /dev/null +++ b/configs/api.go @@ -0,0 +1,38 @@ +package configs + +import ( + "apigame/common/svconfig" + "apigame/common/svconst" + "apigame/service/constd" + "fmt" +) + +type ApiGameConfig struct { + AppId string `json:"appid"` + GameId string `json:"gameid"` + Secret string `json:"secret"` + AppKey string `json:"appkey"` + Name string `json:"name"` +} + +func (c *ApiGameConfig) GetRule(gameId string) *svconfig.ConfigRule { + tableName := constd.MYSQL_TABLE_S_CARDHOLDER_CONFIG + return &svconfig.ConfigRule{ + DbMysql: svconst.DbApi, + TableNameTemplate: svconfig.MYSQL_TABLE_TEMPLATE, + TableName: tableName + gameId, + CacheKey: fmt.Sprintf("%s::%s::%s", svconfig.REDIS_CACHEP_REFIX, tableName, gameId), + CacheTime: 300, + } +} + +func (c *ApiGameConfig) ReadFromMysql(gameId string) *ApiGameConfig { + rule := c.GetRule(gameId) + +} + +func GetApiGameConfig(gameId string) (config *ApiGameConfig, err error) { + //rds := zredis.GetConn() + //zredis.HGetData(rds, player) + return +} diff --git a/configs/index.go b/configs/index.go new file mode 100644 index 0000000..8771377 --- /dev/null +++ b/configs/index.go @@ -0,0 +1,16 @@ +package configs + +import "apigame/common/svconfig" + +func Init() bool { + + return true +} + +func GetConfigRedis[T svconfig.IConfigRule](gameId string) { + +} + +func GetConfigMysql[T svconfig.IConfigRule](gameId string) { + +} diff --git a/configs/registry.go b/configs/registry.go new file mode 100644 index 0000000..27459e6 --- /dev/null +++ b/configs/registry.go @@ -0,0 +1 @@ +package configs diff --git a/main.go b/main.go index 02089e9..9aeea2e 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,13 @@ package main import ( + "apigame/common/svconfig" "apigame/common/svconst" "apigame/common/svdto" "apigame/common/svlog" "apigame/common/svmysql" "apigame/common/svredis" + "apigame/configs" _ "apigame/routers" "apigame/service/cardholder" "github.com/astaxie/beego" @@ -57,6 +59,13 @@ func Init() { // //_ = config.InitLxLimit() + if !svconfig.Init() { + return + } + if !configs.Init() { + return + } + svdto.Init() // 初始化配置 diff --git a/util/zredis/kv.go b/util/zredis/kv.go index d2ac13e..43c54a1 100644 --- a/util/zredis/kv.go +++ b/util/zredis/kv.go @@ -17,3 +17,13 @@ func Increment(key string) int64 { } return 0 } + +func Set(conn redis.Conn, key, value string) (err error) { + _, err = conn.Do("Set", key, value) + return +} + +func Get(conn redis.Conn, key string) (value string, err error) { + value, err = redis.String(conn.Do("Get", "name")) + return +} -- libgit2 0.21.0