-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnew.go
473 lines (409 loc) · 10.8 KB
/
new.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
package ginplus
import (
"context"
"embed"
"errors"
"io/fs"
"net/http"
"os"
"path"
"reflect"
"strings"
"time"
"github.com/aide-cloud/gin-plus/swagger"
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var _ Server = (*GinEngine)(nil)
type HandlerFunc func(controller any, t reflect.Method, req reflect.Type) gin.HandlerFunc
type (
GinEngine struct {
*gin.Engine
// 中间件
middlewares []gin.HandlerFunc
// 控制器
controllers []any
// 绑定前缀和http请求方法的映射
httpMethodPrefixes map[string]httpMethod
// 路由组基础路径
basePath string
// 自定义路由命名规则函数
routeNamingRuleFunc func(methodName string) string
// 自定义handler函数
defaultHandler HandlerFunc
// 自定义Response接口实现
defaultResponse IResponse
// 默认Bind函数
defaultBind func(c *gin.Context, params any) error
// 文档配置
apiConfig ApiConfig
defaultOpenApiYaml string
apiRoutes map[string][]ApiRoute
// 生成API路由开关, 默认为true
genApiEnable bool
// 内置启动地址
// 默认为: :8080
addr string
server *http.Server
// graphql配置
graphqlConfig GraphqlConfig
// prom metrics
metrics *Metrics
// ping
ping *Ping
}
// Metrics prometheus metrics配置
Metrics struct {
Path string
}
// Ping ping配置
Ping struct {
HandlerFunc gin.HandlerFunc
}
// ApiConfig 文档配置,
ApiConfig Info
// RouteNamingRuleFunc 自定义路由命名函数
RouteNamingRuleFunc func(methodName string) string
// IMiddleware 中间件接口, 实现该接口的结构体将会被把中间件添加到该路由组的公共中间件中
IMiddleware interface {
Middlewares() []gin.HandlerFunc
}
// MethodeMiddleware 中间件接口, 为每个方法添加中间件
MethodeMiddleware interface {
MethodeMiddlewares() map[string][]gin.HandlerFunc
}
// Controller 控制器接口, 实现该接口的对象可以自定义模块的路由
Controller interface {
BasePath() string
}
// Route 路由参数结构
Route struct {
Path string
HttpMethod string
Handles []gin.HandlerFunc
}
// ApiRoute api路由参数结构, 用于生成文档
ApiRoute struct {
Path string
HttpMethod string
MethodName string
ReqParams Field
RespParams Field
}
// OptionFun GinEngine配置函数
OptionFun func(*GinEngine)
// httpMethod http请求方法
httpMethod struct {
key string
}
// HttpMethod http请求方法, 绑定前缀和http请求方法的映射
HttpMethod struct {
Prefix string
Method httpMethod
}
// GraphqlConfig graphql配置
GraphqlConfig struct {
// Enable 是否启用
Enable bool
// HandlePath graphql请求路径
HandlePath string
// SchemaPath graphql schema文件路径
ViewPath string
// Root graphql 服务根节点
Root any
// Content graphql schema文件内容
Content embed.FS
}
)
const (
get = "Get"
post = "Post"
put = "Put"
del = "Delete"
patch = "Patch"
head = "Head"
option = "Option"
)
const (
defaultTitle = "github.com/aide-cloud/gin-plus"
defaultVersion = "v0.5.0"
defaultMetricsPath = "/metrics"
defaultPingPath = "/ping"
)
var (
Get = httpMethod{key: get}
Post = httpMethod{key: post}
Put = httpMethod{key: put}
Delete = httpMethod{key: del}
Patch = httpMethod{key: patch}
Head = httpMethod{key: head}
Option = httpMethod{key: option}
)
// defaultPrefixes is the default prefixes.
var defaultPrefixes = map[string]httpMethod{
get: Get,
post: Post,
put: Put,
del: Delete,
patch: Patch,
head: Head,
option: Option,
}
// New 返回一个GinEngine实例
func New(r *gin.Engine, opts ...OptionFun) *GinEngine {
instance := &GinEngine{
Engine: r,
httpMethodPrefixes: defaultPrefixes,
defaultOpenApiYaml: defaultOpenApiYaml,
defaultResponse: NewResponse(),
defaultBind: Bind,
routeNamingRuleFunc: routeToCamel,
apiRoutes: make(map[string][]ApiRoute),
genApiEnable: true,
apiConfig: ApiConfig{
Title: defaultTitle,
Version: defaultVersion,
},
ping: &Ping{HandlerFunc: func(ctx *gin.Context) {
ctx.Status(http.StatusOK)
}},
metrics: &Metrics{Path: defaultMetricsPath},
addr: ":8080",
}
for _, opt := range opts {
opt(instance)
}
if instance.defaultHandler == nil {
instance.defaultHandler = instance.newDefaultHandler
}
instance.Use(instance.middlewares...)
for _, c := range instance.controllers {
instance.genRoute(nil, c, false)
}
return instance
}
// Start 启动HTTP服务器
func (l *GinEngine) Start() error {
if l.server == nil {
//创建HTTP服务器
server := &http.Server{
Addr: l.addr,
Handler: l.Engine,
}
l.server = server
}
//启动HTTP服务器
go func() {
if err := l.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
Logger().Sugar().Infof("[GIN-PLUS] [INFO] Server listen: %s\n", err)
}
}()
Logger().Sugar().Infof("[GIN-PLUS] [INFO] Server is running at %s", l.addr)
return nil
}
// Stop 停止HTTP服务器
func (l *GinEngine) Stop() {
//创建超时上下文,Shutdown可以让未处理的连接在这个时间内关闭
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
//停止HTTP服务器
if err := l.server.Shutdown(ctx); err != nil {
Logger().Sugar().Errorf("[GIN-PLUS] [INFO] Server Shutdown: %v", err)
}
Logger().Sugar().Infof("[GIN-PLUS] [INFO] Server stopped")
}
func registerPing(instance *GinEngine, ping *Ping) {
if ping != nil {
instance.GET(defaultPingPath, ping.HandlerFunc)
return
}
}
func registerMetrics(instance *GinEngine, metrics *Metrics) {
if metrics == nil {
return
}
if metrics.Path == "" {
metrics.Path = defaultMetricsPath
}
instance.GET(metrics.Path, gin.WrapH(promhttp.Handler()))
}
func registerSwaggerUI(instance *GinEngine, enable bool) {
if !enable {
return
}
instance.genOpenApiYaml()
fp, _ := fs.Sub(swagger.Dist, "dist")
instance.StaticFS("/swagger-ui", http.FS(fp))
instance.GET("/openapi/doc/swagger", func(ctx *gin.Context) {
file, _ := os.ReadFile(instance.defaultOpenApiYaml)
ctx.Writer.Header().Set("Content-Type", "text/yaml; charset=utf-8")
_, _ = ctx.Writer.Write(file)
})
}
func registerGraphql(instance *GinEngine, config GraphqlConfig) {
if !config.Enable || config.Root == nil {
return
}
if config.HandlePath == "" {
config.HandlePath = DefaultHandlePath
}
if config.ViewPath == "" {
config.ViewPath = DefaultViewPath
}
instance.POST(config.HandlePath, gin.WrapH(Handler(config.Root, config.Content)))
instance.GET(config.ViewPath, gin.WrapF(View(config.HandlePath)))
}
func (l *GinEngine) RegisterPing(ping ...*Ping) *GinEngine {
if len(ping) > 0 {
l.ping = ping[0]
}
registerPing(l, l.ping)
return l
}
// RegisterMetrics 注册prometheus metrics
func (l *GinEngine) RegisterMetrics(metrics ...*Metrics) *GinEngine {
if len(metrics) > 0 {
l.metrics = metrics[0]
}
registerMetrics(l, l.metrics)
return l
}
// RegisterSwaggerUI 注册swagger ui
func (l *GinEngine) RegisterSwaggerUI() *GinEngine {
registerSwaggerUI(l, l.genApiEnable)
return l
}
// RegisterGraphql 注册graphql
func (l *GinEngine) RegisterGraphql(config ...*GraphqlConfig) *GinEngine {
if len(config) > 0 {
l.graphqlConfig = *config[0]
}
registerGraphql(l, l.graphqlConfig)
return l
}
// WithControllers sets the controllers.
func WithControllers(controllers ...any) OptionFun {
return func(g *GinEngine) {
g.controllers = controllers
}
}
// WithMiddlewares sets the middlewares.
func WithMiddlewares(middlewares ...gin.HandlerFunc) OptionFun {
return func(g *GinEngine) {
g.middlewares = middlewares
}
}
// WithHttpMethodPrefixes sets the prefixes.
func WithHttpMethodPrefixes(prefixes ...HttpMethod) OptionFun {
return func(g *GinEngine) {
prefixHttpMethodMap := make(map[string]httpMethod)
for _, prefix := range prefixes {
if prefix.Prefix == "" || prefix.Method.key == "" {
continue
}
prefixHttpMethodMap[prefix.Prefix] = prefix.Method
}
g.httpMethodPrefixes = prefixHttpMethodMap
}
}
// AppendHttpMethodPrefixes append the prefixes.
func AppendHttpMethodPrefixes(prefixes ...HttpMethod) OptionFun {
return func(g *GinEngine) {
prefixHttpMethodMap := g.httpMethodPrefixes
if prefixHttpMethodMap == nil {
prefixHttpMethodMap = make(map[string]httpMethod)
}
for _, prefix := range prefixes {
if prefix.Prefix == "" || prefix.Method.key == "" {
continue
}
prefixHttpMethodMap[prefix.Prefix] = prefix.Method
}
g.httpMethodPrefixes = prefixHttpMethodMap
}
}
// WithBasePath sets the base path.
func WithBasePath(basePath string) OptionFun {
return func(g *GinEngine) {
g.basePath = path.Join("/", basePath)
}
}
// WithRouteNamingRuleFunc 自定义路由命名函数
func WithRouteNamingRuleFunc(ruleFunc RouteNamingRuleFunc) OptionFun {
return func(g *GinEngine) {
g.routeNamingRuleFunc = ruleFunc
}
}
// WithApiConfig sets the title.
func WithApiConfig(c ApiConfig) OptionFun {
return func(g *GinEngine) {
g.apiConfig = c
}
}
// WithOpenApiYaml 自定义api文件存储位置和文件名称
func WithOpenApiYaml(dir, filename string) OptionFun {
return func(g *GinEngine) {
if !strings.HasSuffix(filename, ".yaml") {
Logger().Sugar().Infof("[GIN-PLUS] [WARNING] filename has no (.yaml) suffix, so the default (%s) is used as the filename.\n", defaultOpenApiYaml)
}
g.defaultOpenApiYaml = path.Join(dir, filename)
}
}
// WithGenApiEnable 设置是否生成API路由
func WithGenApiEnable(enable bool) OptionFun {
return func(g *GinEngine) {
g.genApiEnable = enable
}
}
// WithAddr 设置启动地址
func WithAddr(addr string) OptionFun {
return func(g *GinEngine) {
g.addr = addr
}
}
// WithHttpServer 设置启动地址
func WithHttpServer(server *http.Server) OptionFun {
return func(g *GinEngine) {
server.Handler = g.Engine
if server.Addr == "" {
server.Addr = g.addr
}
g.server = server
}
}
// WithGraphqlConfig 设置graphql配置
func WithGraphqlConfig(config GraphqlConfig) OptionFun {
return func(g *GinEngine) {
g.graphqlConfig = config
}
}
// WithDefaultHandler 自定义handler函数
func WithDefaultHandler(handler HandlerFunc) OptionFun {
return func(g *GinEngine) {
g.defaultHandler = handler
}
}
// WithDefaultResponse 自定义Response接口实现
func WithDefaultResponse(response IResponse) OptionFun {
return func(g *GinEngine) {
g.defaultResponse = response
}
}
// WithMetrics 自定义Metrics
func WithMetrics(metrics *Metrics) OptionFun {
return func(g *GinEngine) {
g.metrics = metrics
}
}
// WithPing 自定义Ping
func WithPing(ping *Ping) OptionFun {
return func(g *GinEngine) {
g.ping = ping
}
}
// WithBind 自定义Bind
func WithBind(bind func(c *gin.Context, params any) error) OptionFun {
return func(g *GinEngine) {
g.defaultBind = bind
}
}