Skip to content

Commit

Permalink
update template
Browse files Browse the repository at this point in the history
  • Loading branch information
topgamesinc committed Jul 24, 2023
1 parent ec1072d commit 6529ccf
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 19 deletions.
9 changes: 2 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ require (
github.com/gin-gonic/gin v1.8.1
github.com/go-playground/validator/v10 v10.14.0
github.com/go-redis/redis v6.15.9+incompatible
github.com/go-redis/redis/v8 v8.11.4
github.com/go-redis/redis/v9 v9.0.0-beta.2
github.com/go-redsync/redsync/v4 v4.6.0
github.com/go-sql-driver/mysql v1.7.0
github.com/golang-jwt/jwt/v4 v4.4.2
github.com/google/go-github/v52 v52.0.0
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75
github.com/gorilla/websocket v1.5.0
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
Expand All @@ -44,7 +44,7 @@ require (
go.uber.org/zap v1.21.0
golang.org/x/image v0.0.0-20220722155232-062f8c9fd539
golang.org/x/net v0.10.0
golang.org/x/oauth2 v0.7.0
golang.org/x/text v0.9.0
google.golang.org/grpc v1.53.0
google.golang.org/protobuf v1.28.1
gopkg.in/natefinch/lumberjack.v2 v2.0.0
Expand All @@ -57,10 +57,8 @@ require (

require (
github.com/HdrHistogram/hdrhistogram-go v1.1.2 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand All @@ -80,7 +78,6 @@ require (
github.com/golang/mock v1.4.4 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
Expand Down Expand Up @@ -132,8 +129,6 @@ require (
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/sync v0.0.0-20220923202941-7f9b1623fab7 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
gopkg.in/ini.v1 v1.66.6 // indirect
)
10 changes: 0 additions & 10 deletions pkg/common/xredis/lock.go

This file was deleted.

5 changes: 5 additions & 0 deletions pkg/common/xredis/redis_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ func TTL(key string) time.Duration {
return Cli.Client.TTL(context.Background(), key).Val()
}

func Del(key string) error {
key = RealKey(key)
return Cli.Client.Del(context.Background(), key).Err()
}

//func Dels(keys ...string) error {
// return Cli.Client.Del(context.Background(), keys...).Err()
//}
Expand Down
17 changes: 17 additions & 0 deletions pkg/common/xredis/redis_lock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package xredis

import (
"context"
"github.com/go-redsync/redsync/v4"
"time"
)

func NewMutex(key string, options ...redsync.Option) *redsync.Mutex {
key = RealKey(key)
return Cli.RedsSync.NewMutex(key, options...)
}

func SetNX(key string, value interface{}, expiration time.Duration) (bool, error) {
key = RealKey(key)
return Cli.Client.SetNX(context.Background(), key, value, expiration).Result()
}
1 change: 1 addition & 0 deletions scripts/gencode/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type GenConfig struct {
Prefix string // 前缀
Suffix string // 后缀
PackageName string // 包名
Filename string // 文件名
FileType int
Dict map[string]interface{}
}
Expand Down
12 changes: 11 additions & 1 deletion scripts/gencode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

func main() {
var (
serviceName = "Discover"
serviceName = "RedPacket"
upperServiceName = toCamel(serviceName)
lowerServiceName = firstLower(upperServiceName)
packageName = camelToSnake(upperServiceName)
Expand Down Expand Up @@ -49,6 +49,7 @@ func main() {
generateDomainPoCode(conf)

// interfaces
generateInterfacesDigCode(conf)
generateInterfacesCtrlCode(conf)
generateInterfacesDtoCode(conf)
generateInterfacesRouterCode(conf)
Expand Down Expand Up @@ -97,6 +98,7 @@ func camelToSnake(input string) string {
// Apps
func generateAppsClientCode(conf config.GenConfig) {
conf.Path = fmt.Sprintf("./apps/%s/client", conf.PackageName)
conf.Filename = "client"
utils.GenCode(template.AppsClientTemplate, &conf)
}

Expand All @@ -110,11 +112,13 @@ func generateAppsConfigCode(conf config.GenConfig) {
conf.Path = fmt.Sprintf("./apps/%s/internal/config", conf.PackageName)
code := strings.ReplaceAll(template.AppsConfigCode, "\"yaml:", "`yaml:")
code = strings.ReplaceAll(code, "\"\"", "\"`")
conf.Filename = "config"
utils.GenCode(template.ParseTemplate(code), &conf)
}

func generateAppsDigCode(conf config.GenConfig) {
conf.Path = fmt.Sprintf("./apps/%s/dig", conf.PackageName)
conf.Filename = "dig"
utils.GenCode(template.AppsDigTemplate, &conf)
}

Expand Down Expand Up @@ -175,6 +179,12 @@ func generateDomainRepoCode(conf config.GenConfig) {
utils.GenCode(template.DomainRepoTemplate, &conf)
}

func generateInterfacesDigCode(conf config.GenConfig) {
conf.Path = "./apps/interfaces/dig"
conf.Prefix = "dig_"
utils.GenCode(template.InterfacesDigTemplate, &conf)
}

func generateInterfacesCtrlCode(conf config.GenConfig) {
conf.Path = fmt.Sprintf("./apps/interfaces/internal/ctrl/ctrl_%s", conf.PackageName)
conf.Prefix = "ctrl_"
Expand Down
2 changes: 2 additions & 0 deletions scripts/gencode/template/template.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package template

import (
"strings"
"text/template"
)

func ParseTemplate(t string) *template.Template {
//if strings.HasPrefix(t, "\n") == true {
// t = t[1:]
//}
t = strings.TrimSpace(t)
tpl, err := template.New("output_template").Parse(t)
if err != nil {
panic(err)
Expand Down
14 changes: 14 additions & 0 deletions scripts/gencode/template/template_interfaces_dig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package template

var InterfacesDigTemplate = ParseTemplate(`
package dig
import (
"lark/apps/interfaces/internal/service/svc_{{.PackageName}}"
)
func provide{{.UpperServiceName}}() {
container.Provide(svc_{{.PackageName}}.New{{.UpperServiceName}}Service)
}
`)
9 changes: 8 additions & 1 deletion scripts/gencode/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ func GenCode(tpl *template.Template, conf *config.GenConfig) (err error) {
}()
var (
wr = new(bytes.Buffer)
path = conf.Path + "/" + conf.Prefix + conf.PackageName + conf.Suffix
path string
filename string
exists bool
formattedCode []byte
)
if conf.Filename != "" {
filename = conf.Filename
} else {
filename = conf.PackageName
}
path = conf.Path + "/" + conf.Prefix + filename + conf.Suffix
switch conf.FileType {
case config.FILE_TYPE_GO:
path += ".go"
Expand Down
19 changes: 19 additions & 0 deletions scripts/redis_lua/load_script_v0.4.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ else
return redis.call('SET', key, 1, 'EX', ex)
end
"

redis-cli -h $SERVER_HOST -p $i script load "
local product_key = KEYS[1]
local total_num_key = ARGV[1]
local sold_num_key = ARGV[2]
local values = redis.call('HMGET', product_key, total_num_key, sold_num_key)
if #values ~= 2 then
return 'PARAM_ERROR'
end
local total_num = tonumber(values[1])
local sold_num = tonumber(values[2])
if total_num > sold_num then
return redis.call('HINCRBY', product_key, sold_num_key, 1)
end
return 0
"

done


Expand Down
22 changes: 22 additions & 0 deletions scripts/redis_lua/spike.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by saeipi.
--- DateTime: 2023/7/21 2:20 PM
---
---

local product_key = KEYS[1]
local total_num_key = ARGV[1]
local sold_num_key = ARGV[2]

local values = redis.call('HMGET', product_key, total_num_key, sold_num_key)
if #values ~= 2 then
return 0
end

local total_num = tonumber(values[1])
local sold_num = tonumber(values[2])
if total_num > sold_num then
return redis.call('HINCRBY', product_key, sold_num_key, 1)
end
return 0
9 changes: 9 additions & 0 deletions scripts/redis_lua/spike_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
local KEYS = {"PK_1001"}
local ARGV = {"TOTAL_NUM","SOLD_NUM"}


local productKey = KEYS[1]
local totalNumKey = ARGV[1]
local soldNumKey = ARGV[2]

print("KEY:",productKey,' TOTAL_NUM:', totalNumKey, " SOLD_NUM:", soldNumKey)

0 comments on commit 6529ccf

Please sign in to comment.