-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup_option.go
90 lines (71 loc) · 2.09 KB
/
setup_option.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
package ginKit
import "github.com/gin-gonic/gin"
type (
ginOptions struct {
// ServiceInfo 当前服务的信息(默认为"")
/*
e.g."Agent-127.0.0.1:12345"
涉及: (a) 限流器
(b) RespondError()、RespondPackage()、RespondPackageOrError()
*/
ServiceInfo string
// RecoveryMiddleware panic恢复中间件(可以为nil,将采用默认值: gin.Recovery())
RecoveryMiddleware gin.HandlerFunc
// DefaultOptionsMiddleware 是否使用 默认的Options中间件?
DefaultOptionsMiddleware bool
// DefaultNoRouteHtml 是否使用 默认的404页面?
DefaultNoRouteHtml bool
// DefaultNoMethod 是否使用 默认的405处理?
DefaultNoMethod bool
// DefaultFavicon 是否使用 默认的favicon.ico?
DefaultFavicon bool
}
GinOption func(opts *ginOptions)
)
func loadOptions(options ...GinOption) *ginOptions {
opts := &ginOptions{
ServiceInfo: "",
RecoveryMiddleware: nil,
DefaultOptionsMiddleware: true,
DefaultNoRouteHtml: true,
DefaultNoMethod: true,
DefaultFavicon: true,
}
for _, option := range options {
option(opts)
}
if opts.RecoveryMiddleware == nil {
opts.RecoveryMiddleware = gin.Recovery()
}
return opts
}
func WithServiceInfo(serviceInfo string) GinOption {
return func(opts *ginOptions) {
opts.ServiceInfo = serviceInfo
}
}
func WithRecoveryMiddleware(recoveryMiddleware gin.HandlerFunc) GinOption {
return func(opts *ginOptions) {
opts.RecoveryMiddleware = recoveryMiddleware
}
}
func WithDefaultOptionsMiddleware(defaultOptionsMiddleware bool) GinOption {
return func(opts *ginOptions) {
opts.DefaultOptionsMiddleware = defaultOptionsMiddleware
}
}
func WithDefaultNoRouteHtml(defaultNoRouteHtml bool) GinOption {
return func(opts *ginOptions) {
opts.DefaultNoRouteHtml = defaultNoRouteHtml
}
}
func WithDefaultNoMethod(defaultNoMethod bool) GinOption {
return func(opts *ginOptions) {
opts.DefaultNoMethod = defaultNoMethod
}
}
func WithDefaultFavicon(defaultFavicon bool) GinOption {
return func(opts *ginOptions) {
opts.DefaultFavicon = defaultFavicon
}
}