Commit ef31119a1aa4befaf2e788309c1d656c8ecbae75
1 parent
35c0874e
Exists in
master
and in
1 other branch
refactor♻️:项目目录重构
Showing
22 changed files
with
306 additions
and
92 deletions
Show diff stats
| ... | ... | @@ -0,0 +1 @@ |
| 1 | +package svconst | ... | ... |
| ... | ... | @@ -0,0 +1,40 @@ |
| 1 | +package svlog | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "apigame/util/util-lx/lxalilog" | |
| 5 | + "fmt" | |
| 6 | + "github.com/astaxie/beego" | |
| 7 | + lalilog "github.com/lixu-any/go-tools/ali/log" | |
| 8 | +) | |
| 9 | + | |
| 10 | +func InitAliLog(appName string) { | |
| 11 | + | |
| 12 | + //初始化阿里云日志 | |
| 13 | + logm, _ := beego.AppConfig.GetSection("alilog") | |
| 14 | + lxalilog.InitAliLog(lxalilog.MapAlilogConfig{ | |
| 15 | + Endpoint: logm["endpoint"], | |
| 16 | + AccessKeyId: logm["accesskeyid"], | |
| 17 | + AccessKeySecret: logm["accesskeysecret"], | |
| 18 | + Project: logm["project"], | |
| 19 | + AppNaame: appName, | |
| 20 | + On: logm["on"], | |
| 21 | + Debug: logm["debug"], | |
| 22 | + TableError: "api_code_merge", | |
| 23 | + TableDebug: logm["tabledebug"], | |
| 24 | + Env: beego.AppConfig.String("env"), | |
| 25 | + }) | |
| 26 | + | |
| 27 | + lalilog.InitAliLog(lalilog.MapAlilogConfig{ | |
| 28 | + Endpoint: logm["endpoint"], | |
| 29 | + AccessKeyId: logm["accesskeyid"], | |
| 30 | + AccessKeySecret: logm["accesskeysecret"], | |
| 31 | + Project: logm["project"], | |
| 32 | + On: logm["on"], | |
| 33 | + Debug: logm["debug"], | |
| 34 | + TableError: logm["tableerror"], | |
| 35 | + TableDebug: logm["tabledebug"], | |
| 36 | + Env: beego.AppConfig.String("env"), | |
| 37 | + }) | |
| 38 | + | |
| 39 | + fmt.Println(appName + " alilog init success") | |
| 40 | +} | ... | ... |
| ... | ... | @@ -0,0 +1,21 @@ |
| 1 | +package svlog | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "apigame/common/svconst" | |
| 5 | + "apigame/util/util-lx/lxbeego" | |
| 6 | + "fmt" | |
| 7 | + "github.com/astaxie/beego" | |
| 8 | +) | |
| 9 | + | |
| 10 | +func Init() { | |
| 11 | + | |
| 12 | + // 初始化日志文件 | |
| 13 | + conf, _ := beego.AppConfig.GetSection("log") | |
| 14 | + conf["filename"] = conf["path"] + "api.log" | |
| 15 | + | |
| 16 | + if err := lxbeego.Inits(conf); err != nil { | |
| 17 | + fmt.Println("svlog.Inits Error::" + err.Error()) | |
| 18 | + } | |
| 19 | + | |
| 20 | + fmt.Println(svconst.AppName + " svlog init success") | |
| 21 | +} | ... | ... |
| ... | ... | @@ -0,0 +1,35 @@ |
| 1 | +package svredis | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "apigame/common/svconst" | |
| 5 | + "apigame/util/utstring" | |
| 6 | + "apigame/util/zredis" | |
| 7 | + "fmt" | |
| 8 | + "github.com/astaxie/beego" | |
| 9 | + "github.com/mitchellh/mapstructure" | |
| 10 | +) | |
| 11 | + | |
| 12 | +type RedisConfig struct { | |
| 13 | + Host string `json:"host"` | |
| 14 | + Port string `json:"port"` | |
| 15 | + Pwd string `json:"pwd"` | |
| 16 | + Db string `json:"Db"` | |
| 17 | + Prefix string `json:"prefix"` | |
| 18 | +} | |
| 19 | + | |
| 20 | +func Init() bool { | |
| 21 | + //初始化Redis | |
| 22 | + confText, _ := beego.AppConfig.GetSection("redis") | |
| 23 | + var conf RedisConfig | |
| 24 | + err := mapstructure.Decode(confText, &conf) | |
| 25 | + if err != nil { | |
| 26 | + fmt.Println("svredis.Init mapstructure.Decode Error::" + err.Error()) | |
| 27 | + } | |
| 28 | + zredis.Init(conf.Host+":"+conf.Port, conf.Pwd, utstring.StringToInt(conf.Db)) | |
| 29 | + if !zredis.Check() { | |
| 30 | + return false | |
| 31 | + } | |
| 32 | + | |
| 33 | + fmt.Println(svconst.AppName + " redis init success") | |
| 34 | + return true | |
| 35 | +} | ... | ... |
common/zconst/consts.go
| ... | ... | @@ -1 +0,0 @@ |
| 1 | -package zconst |
common/zconst/vars.go
common/zlog/ali-log.go
| ... | ... | @@ -1,40 +0,0 @@ |
| 1 | -package zlog | |
| 2 | - | |
| 3 | -import ( | |
| 4 | - "apigame/util/util-lx/lxalilog" | |
| 5 | - "fmt" | |
| 6 | - "github.com/astaxie/beego" | |
| 7 | - lalilog "github.com/lixu-any/go-tools/ali/log" | |
| 8 | -) | |
| 9 | - | |
| 10 | -func InitAliLog(appName string) { | |
| 11 | - | |
| 12 | - //初始化阿里云日志 | |
| 13 | - logm, _ := beego.AppConfig.GetSection("alilog") | |
| 14 | - lxalilog.InitAliLog(lxalilog.MapAlilogConfig{ | |
| 15 | - Endpoint: logm["endpoint"], | |
| 16 | - AccessKeyId: logm["accesskeyid"], | |
| 17 | - AccessKeySecret: logm["accesskeysecret"], | |
| 18 | - Project: logm["project"], | |
| 19 | - AppNaame: appName, | |
| 20 | - On: logm["on"], | |
| 21 | - Debug: logm["debug"], | |
| 22 | - TableError: "api_code_merge", | |
| 23 | - TableDebug: logm["tabledebug"], | |
| 24 | - Env: beego.AppConfig.String("env"), | |
| 25 | - }) | |
| 26 | - | |
| 27 | - lalilog.InitAliLog(lalilog.MapAlilogConfig{ | |
| 28 | - Endpoint: logm["endpoint"], | |
| 29 | - AccessKeyId: logm["accesskeyid"], | |
| 30 | - AccessKeySecret: logm["accesskeysecret"], | |
| 31 | - Project: logm["project"], | |
| 32 | - On: logm["on"], | |
| 33 | - Debug: logm["debug"], | |
| 34 | - TableError: logm["tableerror"], | |
| 35 | - TableDebug: logm["tabledebug"], | |
| 36 | - Env: beego.AppConfig.String("env"), | |
| 37 | - }) | |
| 38 | - | |
| 39 | - fmt.Println(appName + " alilog init success") | |
| 40 | -} |
common/zlog/log.go
| ... | ... | @@ -1,21 +0,0 @@ |
| 1 | -package zlog | |
| 2 | - | |
| 3 | -import ( | |
| 4 | - "apigame/common/zconst" | |
| 5 | - "apigame/util/util-lx/lxbeego" | |
| 6 | - "fmt" | |
| 7 | - "github.com/astaxie/beego" | |
| 8 | -) | |
| 9 | - | |
| 10 | -func Init() { | |
| 11 | - | |
| 12 | - // 初始化日志文件 | |
| 13 | - logconfig, _ := beego.AppConfig.GetSection("log") | |
| 14 | - logconfig["filename"] = logconfig["path"] + "api.log" | |
| 15 | - | |
| 16 | - if err := lxbeego.Inits(logconfig); err != nil { | |
| 17 | - fmt.Println("lxbeelog.Inits Error::" + err.Error()) | |
| 18 | - } | |
| 19 | - | |
| 20 | - fmt.Println(zconst.AppName + " beelog init success") | |
| 21 | -} |
common/zredis/index.go
| ... | ... | @@ -1 +0,0 @@ |
| 1 | -package zredis |
dto/inits.go
| ... | ... | @@ -11,22 +11,6 @@ func Inits() (err error) { |
| 11 | 11 | |
| 12 | 12 | appname := beego.AppConfig.String("appname") |
| 13 | 13 | |
| 14 | - ////初始化Redis | |
| 15 | - //redisconfig, _ := beego.AppConfig.GetSection("redis") | |
| 16 | - //var mredisconfig lxredis.MapRedisConfig | |
| 17 | - //err = mapstructure.Decode(redisconfig, &mredisconfig) | |
| 18 | - //if err != nil { | |
| 19 | - // fmt.Println(" mapstructure.Decode Error::" + err.Error()) | |
| 20 | - //} else { | |
| 21 | - // err = lxredis.InitRedis(mredisconfig) | |
| 22 | - // if err != nil { | |
| 23 | - // fmt.Println(" lxredis.InitRedis Error::" + err.Error()) | |
| 24 | - // return err | |
| 25 | - // } | |
| 26 | - //} | |
| 27 | - // | |
| 28 | - //fmt.Println(appname + " redis init success") | |
| 29 | - | |
| 30 | 14 | //初始化MYSQL |
| 31 | 15 | var conns []lxmysql.MapMysqlConn |
| 32 | 16 | ... | ... |
go.mod
| ... | ... | @@ -8,7 +8,9 @@ require ( |
| 8 | 8 | github.com/astaxie/beego v1.12.3 |
| 9 | 9 | github.com/garyburd/redigo v1.6.4 |
| 10 | 10 | github.com/go-sql-driver/mysql v1.7.0 |
| 11 | + github.com/gomodule/redigo v2.0.0+incompatible | |
| 11 | 12 | github.com/lixu-any/go-tools v0.0.0-20221128094643-07d54c69e9bf |
| 13 | + github.com/mitchellh/mapstructure v1.1.2 | |
| 12 | 14 | gorm.io/driver/mysql v1.5.6 |
| 13 | 15 | gorm.io/gorm v1.25.9 |
| 14 | 16 | ) | ... | ... |
go.sum
| ... | ... | @@ -156,6 +156,7 @@ github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0 |
| 156 | 156 | github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= |
| 157 | 157 | github.com/golang/snappy v0.0.0-20170215233205-553a64147049/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= |
| 158 | 158 | github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= |
| 159 | +github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0= | |
| 159 | 160 | github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4= |
| 160 | 161 | github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= |
| 161 | 162 | github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= |
| ... | ... | @@ -251,6 +252,7 @@ github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eI |
| 251 | 252 | github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg= |
| 252 | 253 | github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY= |
| 253 | 254 | github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= |
| 255 | +github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= | |
| 254 | 256 | github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= |
| 255 | 257 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= |
| 256 | 258 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= | ... | ... |
main.go
| 1 | 1 | package main |
| 2 | 2 | |
| 3 | 3 | import ( |
| 4 | - "apigame/common/zconst" | |
| 5 | - "apigame/common/zlog" | |
| 4 | + "apigame/common/svconst" | |
| 5 | + "apigame/common/svlog" | |
| 6 | + "apigame/common/svredis" | |
| 6 | 7 | "apigame/dto" |
| 7 | 8 | _ "apigame/routers" |
| 8 | 9 | "apigame/service/cardholder" |
| ... | ... | @@ -36,13 +37,16 @@ func main() { |
| 36 | 37 | |
| 37 | 38 | func Init() { |
| 38 | 39 | |
| 39 | - zconst.AppName = beego.AppConfig.String("appname") | |
| 40 | + svconst.AppName = beego.AppConfig.String("appname") | |
| 40 | 41 | |
| 41 | - zlog.Init() | |
| 42 | + svlog.Init() | |
| 43 | + svlog.InitAliLog(svconst.AppName) | |
| 42 | 44 | |
| 43 | - _ = dto.Inits() | |
| 45 | + if !svredis.Init() { | |
| 46 | + return | |
| 47 | + } | |
| 44 | 48 | |
| 45 | - zlog.InitAliLog(zconst.AppName) | |
| 49 | + _ = dto.Inits() | |
| 46 | 50 | |
| 47 | 51 | // 初始化配置 |
| 48 | 52 | cardholder.Init() | ... | ... |
service/cardholder/logic.go
| ... | ... | @@ -6,9 +6,9 @@ import ( |
| 6 | 6 | "apigame/service/constd" |
| 7 | 7 | "apigame/util/util-lx/lxalilog" |
| 8 | 8 | "apigame/util/util-lx/lxtime" |
| 9 | - "apigame/util/utjson" | |
| 10 | 9 | "apigame/util/utslice" |
| 11 | 10 | "apigame/util/utstring" |
| 11 | + "apigame/util/zjson" | |
| 12 | 12 | ) |
| 13 | 13 | |
| 14 | 14 | // SaveData 存储数据 |
| ... | ... | @@ -229,7 +229,7 @@ func DoOpen(gameId string, |
| 229 | 229 | { |
| 230 | 230 | recordBase := dto.NewCardHolderRecordBase(gameData.Uid, sequenceId, cohort, config.Id, gameData.Details.Round) |
| 231 | 231 | dto.SaveRecord(gameId, dto.NewCardHolderRecordOpen(recordBase, |
| 232 | - openMode, utstring.StringToInt(confCardholder.Id), utjson.JsonString(newCards))) | |
| 232 | + openMode, utstring.StringToInt(confCardholder.Id), zjson.Str(newCards))) | |
| 233 | 233 | } |
| 234 | 234 | |
| 235 | 235 | return | ... | ... |
| ... | ... | @@ -0,0 +1,29 @@ |
| 1 | +package zjson | |
| 2 | + | |
| 3 | +import "encoding/json" | |
| 4 | + | |
| 5 | +func Byte(obj any) (buf []byte) { | |
| 6 | + jsons, err := json.Marshal(obj) | |
| 7 | + if err == nil { | |
| 8 | + buf = jsons | |
| 9 | + } else { | |
| 10 | + } | |
| 11 | + return | |
| 12 | +} | |
| 13 | + | |
| 14 | +func Str(obj any) (text string) { | |
| 15 | + jsons, err := json.Marshal(obj) | |
| 16 | + if err == nil { | |
| 17 | + text = string(jsons) | |
| 18 | + } else { | |
| 19 | + } | |
| 20 | + return | |
| 21 | +} | |
| 22 | + | |
| 23 | +func Obj(text string, obj any) (err error) { | |
| 24 | + err = json.Unmarshal([]byte(text), &obj) | |
| 25 | + if err != nil { | |
| 26 | + | |
| 27 | + } | |
| 28 | + return | |
| 29 | +} | ... | ... |
| ... | ... | @@ -0,0 +1,30 @@ |
| 1 | +package zredis | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "fmt" | |
| 5 | + "github.com/gomodule/redigo/redis" | |
| 6 | +) | |
| 7 | + | |
| 8 | +func Check() bool { | |
| 9 | + if pool == nil { | |
| 10 | + fmt.Println("redis pool nil") | |
| 11 | + } | |
| 12 | + conn := pool.Get() | |
| 13 | + defer AutoClose(conn) | |
| 14 | + | |
| 15 | + _, err := redis.Int64(conn.Do("GET", "test")) | |
| 16 | + if err != nil { | |
| 17 | + if err == redis.ErrNil { | |
| 18 | + //fmt.Println("redis.ErrNil") | |
| 19 | + } else { | |
| 20 | + fmt.Println("err", err) | |
| 21 | + fmt.Println("redis conn error", | |
| 22 | + "_addr", _addr, | |
| 23 | + "_pass", _pass, | |
| 24 | + "_db", _db) | |
| 25 | + return false | |
| 26 | + } | |
| 27 | + } | |
| 28 | + | |
| 29 | + return true | |
| 30 | +} | ... | ... |
| ... | ... | @@ -0,0 +1,29 @@ |
| 1 | +package zredis | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "github.com/gomodule/redigo/redis" | |
| 5 | +) | |
| 6 | + | |
| 7 | +// HSet HSet | |
| 8 | +func HSet(conn redis.Conn, tableName, key, value any) error { | |
| 9 | + _, err := conn.Do("HSET", tableName, key, value) | |
| 10 | + return err | |
| 11 | +} | |
| 12 | + | |
| 13 | +// HGet HGet | |
| 14 | +func HGet(conn redis.Conn, tableName, key any) any { | |
| 15 | + reply, err := conn.Do("HGET", tableName, key) | |
| 16 | + if err != nil { | |
| 17 | + return nil | |
| 18 | + } | |
| 19 | + return reply | |
| 20 | +} | |
| 21 | + | |
| 22 | +// HGetInt64 HGetInt64 | |
| 23 | +func HGetInt64(conn redis.Conn, tableName, key any) int64 { | |
| 24 | + reply, err := redis.Int64(conn.Do("HGET", tableName, key)) | |
| 25 | + if err == nil { | |
| 26 | + return reply | |
| 27 | + } | |
| 28 | + return 0 | |
| 29 | +} | ... | ... |
| ... | ... | @@ -0,0 +1,24 @@ |
| 1 | +package zredis | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "apigame/util/zjson" | |
| 5 | + "github.com/gomodule/redigo/redis" | |
| 6 | +) | |
| 7 | + | |
| 8 | +// HSetData HSetData | |
| 9 | +func HSetData(conn redis.Conn, value IData) error { | |
| 10 | + _, err := conn.Do("HSET", value.TableName(), value.DbKey(), value.ToString()) | |
| 11 | + return err | |
| 12 | +} | |
| 13 | + | |
| 14 | +// HGetData HSetData | |
| 15 | +func HGetData(conn redis.Conn, value IData) IData { | |
| 16 | + text, err := redis.String(conn.Do("HGET", value.TableName(), value.DbKey())) | |
| 17 | + if err == nil { | |
| 18 | + errJson := zjson.Obj(text, value) | |
| 19 | + if errJson != nil { | |
| 20 | + return nil | |
| 21 | + } | |
| 22 | + } | |
| 23 | + return value | |
| 24 | +} | ... | ... |
| ... | ... | @@ -0,0 +1,19 @@ |
| 1 | +package zredis | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "github.com/gomodule/redigo/redis" | |
| 5 | +) | |
| 6 | + | |
| 7 | +func Increment(key string) int64 { | |
| 8 | + conn := GetConn() | |
| 9 | + if conn == nil { | |
| 10 | + return 0 | |
| 11 | + } | |
| 12 | + defer conn.Close() | |
| 13 | + | |
| 14 | + value, err := redis.Int64(conn.Do("INCR", key)) | |
| 15 | + if err == nil { | |
| 16 | + return value | |
| 17 | + } | |
| 18 | + return 0 | |
| 19 | +} | ... | ... |
| ... | ... | @@ -0,0 +1,50 @@ |
| 1 | +package zredis | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "github.com/gomodule/redigo/redis" | |
| 5 | + "time" | |
| 6 | +) | |
| 7 | + | |
| 8 | +var ( | |
| 9 | + _addr string | |
| 10 | + _pass string | |
| 11 | + _db int | |
| 12 | +) | |
| 13 | +var pool *redis.Pool | |
| 14 | + | |
| 15 | +func Init(addr, pass string, db int) { | |
| 16 | + | |
| 17 | + _addr, _pass, _db = addr, pass, db | |
| 18 | + | |
| 19 | + pool = &redis.Pool{ | |
| 20 | + MaxActive: 10240, | |
| 21 | + MaxIdle: 10240, // Maximum number of idle connections in the pool. | |
| 22 | + IdleTimeout: 240 * time.Second, // Close connections after remaining idle for this duration | |
| 23 | + Dial: dial, | |
| 24 | + Wait: true, | |
| 25 | + } | |
| 26 | +} | |
| 27 | + | |
| 28 | +func dial() (redis.Conn, error) { | |
| 29 | + conn, err := redis.Dial("tcp", _addr, | |
| 30 | + redis.DialPassword(_pass), | |
| 31 | + redis.DialDatabase(_db), | |
| 32 | + redis.DialConnectTimeout(5*time.Second), | |
| 33 | + redis.DialReadTimeout(5*time.Second), | |
| 34 | + redis.DialWriteTimeout(5*time.Second), | |
| 35 | + ) | |
| 36 | + return conn, err | |
| 37 | +} | |
| 38 | + | |
| 39 | +func GetConn() redis.Conn { | |
| 40 | + if pool == nil { | |
| 41 | + return nil | |
| 42 | + } | |
| 43 | + return pool.Get() | |
| 44 | +} | |
| 45 | + | |
| 46 | +func AutoClose(conn redis.Conn) { | |
| 47 | + err := conn.Close() | |
| 48 | + if err != nil { | |
| 49 | + } | |
| 50 | +} | ... | ... |