-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.go
107 lines (84 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package httpserve
import "path"
func newGroup(r *Router, route string, hs ...Handler) *group {
var g group
g.r = r
g.route = route
g.hs = hs
return &g
}
// Group represents a handler group
type group struct {
r *Router
route string
hs []Handler
}
// GET will set a GET endpoint
func (g *group) GET(route string, hs ...Handler) {
if g.route != "" {
route = path.Join(g.route, route)
}
if len(g.hs) > 0 {
hs = append(g.hs, hs...)
}
g.r.GET(route, newHandler(hs))
}
// PUT will set a PUT endpoint
func (g *group) PUT(route string, hs ...Handler) {
if g.route != "" {
route = path.Join(g.route, route)
}
if len(g.hs) > 0 {
hs = append(g.hs, hs...)
}
g.r.PUT(route, newHandler(hs))
}
// POST will set a POST endpoint
func (g *group) POST(route string, hs ...Handler) {
if g.route != "" {
route = path.Join(g.route, route)
}
if len(g.hs) > 0 {
hs = append(g.hs, hs...)
}
g.r.POST(route, newHandler(hs))
}
// DELETE will set a DELETE endpoint
func (g *group) DELETE(route string, hs ...Handler) {
if g.route != "" {
route = path.Join(g.route, route)
}
if len(g.hs) > 0 {
hs = append(g.hs, hs...)
}
g.r.DELETE(route, newHandler(hs))
}
// OPTIONS will set a OPTIONS endpoint
func (g *group) OPTIONS(route string, hs ...Handler) {
if g.route != "" {
route = path.Join(g.route, route)
}
if len(g.hs) > 0 {
hs = append(g.hs, hs...)
}
g.r.OPTIONS(route, newHandler(hs))
}
// Group will return a new group
func (g *group) Group(route string, hs ...Handler) Group {
if g.route != "" {
route = path.Join(g.route, route)
}
if len(g.hs) > 0 {
hs = append(g.hs, hs...)
}
return newGroup(g.r, route, hs...)
}
// Group is a grouping interface
type Group interface {
GET(route string, hs ...Handler)
POST(route string, hs ...Handler)
PUT(route string, hs ...Handler)
DELETE(route string, hs ...Handler)
OPTIONS(route string, hs ...Handler)
Group(route string, hs ...Handler) Group
}