Skip to content

Commit c05cfad

Browse files
author
奇淼(piexlmax
authored
Merge pull request #567 from flipped-aurora/gva_gormv2_dev
Gva gormv2 dev
2 parents 6869c93 + 8f3291e commit c05cfad

File tree

12 files changed

+261
-416
lines changed

12 files changed

+261
-416
lines changed

README-en.md

Lines changed: 107 additions & 174 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 109 additions & 193 deletions
Large diffs are not rendered by default.

server/Dockerfile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
FROM golang:alpine
22

3-
ENV GO111MODULE=on
4-
ENV GOPROXY=https://goproxy.io,direct
53
WORKDIR /go/src/gin-vue-admin
64
COPY . .
75

8-
RUN go env && go build -o server .
6+
RUN go generate && go env && go build -o server .
97

108
FROM alpine:latest
119
LABEL MAINTAINER="SliverHorn@[email protected]"

server/global/global.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package global
33
import (
44
"gin-vue-admin/utils/timer"
55

6+
"golang.org/x/sync/singleflight"
7+
68
"go.uber.org/zap"
79

810
"gin-vue-admin/config"
@@ -18,6 +20,7 @@ var (
1820
GVA_CONFIG config.Server
1921
GVA_VP *viper.Viper
2022
//GVA_LOG *oplogging.Logger
21-
GVA_LOG *zap.Logger
22-
GVA_Timer timer.Timer = timer.NewTimerTask()
23+
GVA_LOG *zap.Logger
24+
GVA_Timer timer.Timer = timer.NewTimerTask()
25+
GVA_Concurrency_Control = &singleflight.Group{}
2326
)

server/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ require (
5151
github.com/unrolled/secure v1.0.7
5252
go.uber.org/zap v1.10.0
5353
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect
54+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
5455
golang.org/x/tools v0.0.0-20200324003944-a576cf524670 // indirect
5556
google.golang.org/protobuf v1.24.0 // indirect
5657
gopkg.in/ini.v1 v1.55.0 // indirect

server/middleware/jwt.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import (
77
"gin-vue-admin/model/request"
88
"gin-vue-admin/model/response"
99
"gin-vue-admin/service"
10+
"strconv"
11+
"time"
12+
1013
"github.com/dgrijalva/jwt-go"
1114
"github.com/gin-gonic/gin"
1215
"go.uber.org/zap"
13-
"strconv"
14-
"time"
1516
)
1617

1718
func JWTAuth() gin.HandlerFunc {
@@ -48,7 +49,7 @@ func JWTAuth() gin.HandlerFunc {
4849
}
4950
if claims.ExpiresAt-time.Now().Unix() < claims.BufferTime {
5051
claims.ExpiresAt = time.Now().Unix() + global.GVA_CONFIG.JWT.ExpiresTime
51-
newToken, _ := j.CreateToken(*claims)
52+
newToken, _ := j.CreateTokenByOldToken(token, *claims)
5253
newClaims, _ := j.ParseToken(newToken)
5354
c.Header("new-token", newToken)
5455
c.Header("new-expires-at", strconv.FormatInt(newClaims.ExpiresAt, 10))
@@ -91,6 +92,14 @@ func (j *JWT) CreateToken(claims request.CustomClaims) (string, error) {
9192
return token.SignedString(j.SigningKey)
9293
}
9394

95+
// CreateTokenByOldToken 旧token 换新token 使用归并回源避免并发问题
96+
func (j *JWT) CreateTokenByOldToken(oldToken string, claims request.CustomClaims) (string, error) {
97+
v, err, _ := global.GVA_Concurrency_Control.Do("JWT:"+oldToken, func() (interface{}, error) {
98+
return j.CreateToken(claims)
99+
})
100+
return v.(string), err
101+
}
102+
94103
// 解析 token
95104
func (j *JWT) ParseToken(tokenString string) (*request.CustomClaims, error) {
96105
token, err := jwt.ParseWithClaims(tokenString, &request.CustomClaims{}, func(token *jwt.Token) (i interface{}, e error) {

server/model/sys_api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ type SysApi struct {
99
Path string `json:"path" gorm:"comment:api路径"` // api路径
1010
Description string `json:"description" gorm:"comment:api中文描述"` // api中文描述
1111
ApiGroup string `json:"apiGroup" gorm:"comment:api组"` // api组
12-
Method string `json:"method" gorm:"default:POST" gorm:"comment:方法"` // 方法:创建POST(默认)|查看GET|更新PUT|删除DELETE
12+
Method string `json:"method" gorm:"default:POST;comment:方法"` // 方法:创建POST(默认)|查看GET|更新PUT|删除DELETE
1313
}

server/resource/template/server/api.go.tpl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"go.uber.org/zap"
1111
)
1212

13+
// Create{{.StructName}} 创建{{.StructName}}
1314
// @Tags {{.StructName}}
1415
// @Summary 创建{{.StructName}}
1516
// @Security ApiKeyAuth
@@ -29,6 +30,7 @@ func Create{{.StructName}}(c *gin.Context) {
2930
}
3031
}
3132

33+
// Delete{{.StructName}} 删除{{.StructName}}
3234
// @Tags {{.StructName}}
3335
// @Summary 删除{{.StructName}}
3436
// @Security ApiKeyAuth
@@ -48,6 +50,7 @@ func Delete{{.StructName}}(c *gin.Context) {
4850
}
4951
}
5052

53+
// Delete{{.StructName}}ByIds 批量删除{{.StructName}}
5154
// @Tags {{.StructName}}
5255
// @Summary 批量删除{{.StructName}}
5356
// @Security ApiKeyAuth
@@ -67,6 +70,7 @@ func Delete{{.StructName}}ByIds(c *gin.Context) {
6770
}
6871
}
6972

73+
// Update{{.StructName}} 更新{{.StructName}}
7074
// @Tags {{.StructName}}
7175
// @Summary 更新{{.StructName}}
7276
// @Security ApiKeyAuth
@@ -86,6 +90,7 @@ func Update{{.StructName}}(c *gin.Context) {
8690
}
8791
}
8892

93+
// Find{{.StructName}} 用id查询{{.StructName}}
8994
// @Tags {{.StructName}}
9095
// @Summary 用id查询{{.StructName}}
9196
// @Security ApiKeyAuth
@@ -105,6 +110,7 @@ func Find{{.StructName}}(c *gin.Context) {
105110
}
106111
}
107112

113+
// Get{{.StructName}}List 分页获取{{.StructName}}列表
108114
// @Tags {{.StructName}}
109115
// @Summary 分页获取{{.StructName}}列表
110116
// @Security ApiKeyAuth

server/resource/template/server/model.go.tpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"gin-vue-admin/global"
66
)
77

8+
// {{.StructName}} 结构体
89
// 如果含有time.Time 请自行import time包
910
type {{.StructName}} struct {
1011
global.GVA_MODEL {{- range .Fields}}
@@ -16,6 +17,7 @@ type {{.StructName}} struct {
1617
}
1718

1819
{{ if .TableName }}
20+
// TableName {{.StructName}} 表名
1921
func ({{.StructName}}) TableName() string {
2022
return "{{.TableName}}"
2123
}

server/resource/template/server/router.go.tpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/gin-gonic/gin"
77
)
88

9+
// Init{{.StructName}}Router 初始化 {{.StructName}} 路由信息
910
func Init{{.StructName}}Router(Router *gin.RouterGroup) {
1011
{{.StructName}}Router := Router.Group("{{.Abbreviation}}").Use(middleware.OperationRecord())
1112
{

0 commit comments

Comments
 (0)