-
Notifications
You must be signed in to change notification settings - Fork 18
/
group.go
87 lines (74 loc) · 1.91 KB
/
group.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
package swagin
import (
"github.com/gin-gonic/gin"
"github.com/long2ice/swagin/router"
"github.com/long2ice/swagin/security"
"net/http"
)
type Group struct {
*SwaGin
Path string
Tags []string
Handlers []gin.HandlerFunc
Securities []security.ISecurity
}
type Option func(*Group)
func Handlers(handlers ...gin.HandlerFunc) Option {
return func(g *Group) {
g.Handlers = append(g.Handlers, handlers...)
}
}
func Tags(tags ...string) Option {
return func(g *Group) {
if g.Tags == nil {
g.Tags = tags
} else {
g.Tags = append(g.Tags, tags...)
}
}
}
func Security(securities ...security.ISecurity) Option {
return func(g *Group) {
g.Securities = append(g.Securities, securities...)
}
}
func (g *Group) Handle(path string, method string, r *router.Router) {
router.Handlers(g.Handlers...)(r)
router.Tags(g.Tags...)(r)
router.Security(g.Securities...)(r)
g.SwaGin.Handle(g.Path+path, method, r)
}
func (g *Group) GET(path string, router *router.Router) {
g.Handle(path, http.MethodGet, router)
}
func (g *Group) POST(path string, router *router.Router) {
g.Handle(path, http.MethodPost, router)
}
func (g *Group) HEAD(path string, router *router.Router) {
g.Handle(path, http.MethodHead, router)
}
func (g *Group) PATCH(path string, router *router.Router) {
g.Handle(path, http.MethodPatch, router)
}
func (g *Group) DELETE(path string, router *router.Router) {
g.Handle(path, http.MethodDelete, router)
}
func (g *Group) PUT(path string, router *router.Router) {
g.Handle(path, http.MethodPut, router)
}
func (g *Group) OPTIONS(path string, router *router.Router) {
g.Handle(path, http.MethodOptions, router)
}
func (g *Group) Group(path string, options ...Option) *Group {
group := &Group{
SwaGin: g.SwaGin,
Path: g.Path + path,
Tags: g.Tags,
Handlers: g.Handlers,
Securities: g.Securities,
}
for _, option := range options {
option(group)
}
return group
}