/
group.go
80 lines (68 loc) · 2.05 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
package min
import (
"net/http"
"path"
)
// Group represents a route group that shares a middleware chain and a common
// path.
type Group struct {
Path string
handler Handler
parent *Group
chain Middleware
}
// NewGroup creates a new subgroup of group g.
func (g *Group) NewGroup(path string) *Group {
return &Group{
Path: path,
handler: g.handler,
parent: g,
}
}
// Parent gets the group's parent in the group tree.
func (g *Group) Parent() *Group {
return g.parent
}
// FullPath returns the group's full path in the group tree (as opposed to this
// group's sub-path)
func (g *Group) FullPath() string {
if g.parent == nil {
return g.Path
}
return path.Join(g.parent.FullPath(), g.Path)
}
// Use sets this group's middleware chain. Each call to Use appends to the
// chain.
func (g *Group) Use(m Middleware) {
if g.chain == nil {
g.chain = m
return
}
g.chain = g.chain.Then(m)
}
func (g *Group) handle(method, relativePath string, handler http.Handler) {
if g.chain != nil {
handler = g.chain(handler)
}
g.handler.Handle(method, path.Join(g.FullPath(), relativePath), handler)
}
// Get registers a handler for GET requests on the given relative path.
func (g *Group) Get(relativePath string, handler http.Handler) {
g.handle(http.MethodGet, relativePath, handler)
}
// Post registers a handler for POST requests on the given relative path.
func (g *Group) Post(relativePath string, handler http.Handler) {
g.handle(http.MethodPost, relativePath, handler)
}
// Put registers a handler for PUT requests on the given relative path.
func (g *Group) Put(relativePath string, handler http.Handler) {
g.handle(http.MethodPut, relativePath, handler)
}
// Patch registers a handler for PATCH requests on the given relative path.
func (g *Group) Patch(relativePath string, handler http.Handler) {
g.handle(http.MethodPatch, relativePath, handler)
}
// Delete registers a handler for DELETE requests on the given relative path.
func (g *Group) Delete(relativePath string, handler http.Handler) {
g.handle(http.MethodDelete, relativePath, handler)
}