-
Notifications
You must be signed in to change notification settings - Fork 1
/
routergroup.go
140 lines (121 loc) · 4.22 KB
/
routergroup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package kid
import (
"net/http"
"github.com/gin-gonic/gin"
)
const (
ResponseTypeKey = "resp_type"
)
type ResponseType int8
const (
ResponseTypeJson = iota + 1 // 1 json 类型
ResponseTypeXml // 2 xml 类型
ResponseTypeYaml // 3 yaml 类型
ResponseTypeHtml // 4 html 类型
ResponseTypeIndentedJson // 5 格式化 json
ResponseTypeSecureJson // 6 安全的 json
ResponseTypeJsonP // 7 返回 jsonp 格式
ResponseTypeAsciiJson // 8 返回 Ascii 码的 json 会将 Unicode 转换为 ASCII
ResponseTypePureJson // 9 pure json 格式化
ResponseTypeToml // 10 分会 toml 格式
ResponseTypeProtoBuf // 11 以 protobuf 格式输出
ResponseTypeString // 12 以字符串格式输出
ResponseTypeRedirect // 13 重定向
ResponseTypeFile // 14 文件
ResponseTypeFileFromFS // 15 远程文件系统
ResponseTypeFileAttachment // 16 FileAttachment
ResponseTypeSSEvent // 17 SSEvent
ResponseTypeStream // 18 数据流
)
type RouterGroup struct {
*gin.RouterGroup
}
type HandleFunc func(*Context) any
type Middleware func(*Context)
func (group *RouterGroup) POST(path string, handler HandleFunc, middlewares ...Middleware) {
group.RouterGroup.POST(path, convert(handler, middlewares...)...)
}
func (group *RouterGroup) GET(path string, handler HandleFunc, middlewares ...Middleware) {
group.RouterGroup.GET(path, convert(handler, middlewares...)...)
}
func (group *RouterGroup) PUT(path string, handler HandleFunc, middlewares ...Middleware) {
group.RouterGroup.PUT(path, convert(handler, middlewares...)...)
}
func (group *RouterGroup) DELETE(path string, handler HandleFunc, middlewares ...Middleware) {
group.RouterGroup.DELETE(path, convert(handler, middlewares...)...)
}
func (group *RouterGroup) Group(path string, middlewares ...Middleware) *RouterGroup {
return &RouterGroup{group.RouterGroup.Group(path, convertMiddleware(middlewares...)...)}
}
func (group *RouterGroup) UseMiddle(middlewares ...Middleware) {
group.RouterGroup.Use(convertMiddleware(middlewares...)...)
}
func WrapHandler(handler gin.HandlerFunc) HandleFunc {
return func(ctx *Context) any {
handler(ctx.Context)
return nil
}
}
func convert(handler HandleFunc, middlewares ...Middleware) []gin.HandlerFunc {
h := convertHandleFunc(handler)
ms := convertMiddleware(middlewares...)
return append(ms, h)
}
func convertHandleFunc(handler HandleFunc) gin.HandlerFunc {
return func(ctx *gin.Context) {
kidCtx := &Context{Context: ctx}
resp := handler(kidCtx)
if resp == nil {
ctx.Abort()
return
}
responseData(ctx, kidCtx, resp)
}
}
// responseData 通过获取 ctx 的 resp_type key 确定响应的格式类型
func responseData(ctx *gin.Context, kidCtx *Context, resp interface{}) {
switch kidCtx.GetResponseType() {
case ResponseTypeJson:
ctx.JSON(http.StatusOK, resp)
case ResponseTypeXml:
ctx.XML(http.StatusOK, resp)
case ResponseTypeYaml:
ctx.YAML(http.StatusOK, resp)
case ResponseTypeHtml:
ctx.HTML(http.StatusOK, "", resp)
case ResponseTypeIndentedJson:
ctx.IndentedJSON(http.StatusOK, resp)
case ResponseTypeSecureJson:
ctx.SecureJSON(http.StatusOK, resp)
case ResponseTypeJsonP:
ctx.JSONP(http.StatusOK, resp)
case ResponseTypeAsciiJson:
ctx.AsciiJSON(http.StatusOK, resp)
case ResponseTypePureJson:
ctx.PureJSON(http.StatusOK, resp)
case ResponseTypeToml:
ctx.TOML(http.StatusOK, resp)
case ResponseTypeProtoBuf:
ctx.ProtoBuf(http.StatusOK, resp)
case ResponseTypeString:
ctx.String(http.StatusOK, "", resp)
case ResponseTypeRedirect:
url := resp.(string)
ctx.Redirect(http.StatusOK, url)
case ResponseTypeFile:
filepath := resp.(string)
ctx.File(filepath)
default:
ctx.JSON(http.StatusOK, resp)
}
}
func convertMiddleware(middlewares ...Middleware) []gin.HandlerFunc {
var ginMiddlewares = make([]gin.HandlerFunc, len(middlewares))
for i := range middlewares {
handler := middlewares[i]
ginMiddlewares[i] = func(ctx *gin.Context) {
handler(&Context{Context: ctx})
}
}
return ginMiddlewares
}