-
Notifications
You must be signed in to change notification settings - Fork 13
/
group.go
175 lines (146 loc) · 4.64 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
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
package pure
import (
"net/http"
"strconv"
"strings"
)
// IRouteGroup interface for router group
type IRouteGroup interface {
IRoutes
GroupWithNone(prefix string) IRouteGroup
GroupWithMore(prefix string, middleware ...Middleware) IRouteGroup
Group(prefix string) IRouteGroup
}
// IRoutes interface for routes
type IRoutes interface {
Use(...Middleware)
Any(string, http.HandlerFunc)
Get(string, http.HandlerFunc)
Post(string, http.HandlerFunc)
Delete(string, http.HandlerFunc)
Patch(string, http.HandlerFunc)
Put(string, http.HandlerFunc)
Options(string, http.HandlerFunc)
Head(string, http.HandlerFunc)
Connect(string, http.HandlerFunc)
Trace(string, http.HandlerFunc)
}
// routeGroup struct containing all fields and methods for use.
type routeGroup struct {
prefix string
middleware []Middleware
pure *Mux
}
var _ IRouteGroup = &routeGroup{}
func (g *routeGroup) handle(method string, path string, handler http.HandlerFunc) {
if i := strings.Index(path, "//"); i != -1 {
panic("Bad path '" + path + "' contains duplicate // at index:" + strconv.Itoa(i))
}
h := handler
for i := len(g.middleware) - 1; i >= 0; i-- {
h = g.middleware[i](h)
}
tree := g.pure.trees[method]
if tree == nil {
tree = new(node)
g.pure.trees[method] = tree
}
pCount := tree.add(g.prefix+path, h)
pCount++
if pCount > g.pure.mostParams {
g.pure.mostParams = pCount
}
}
// Use adds a middleware handler to the group middleware chain.
func (g *routeGroup) Use(m ...Middleware) {
g.middleware = append(g.middleware, m...)
}
// Connect adds a CONNECT route & handler to the router.
func (g *routeGroup) Connect(path string, h http.HandlerFunc) {
g.handle(http.MethodConnect, path, h)
}
// Delete adds a DELETE route & handler to the router.
func (g *routeGroup) Delete(path string, h http.HandlerFunc) {
g.handle(http.MethodDelete, path, h)
}
// Get adds a GET route & handler to the router.
func (g *routeGroup) Get(path string, h http.HandlerFunc) {
g.handle(http.MethodGet, path, h)
}
// Head adds a HEAD route & handler to the router.
func (g *routeGroup) Head(path string, h http.HandlerFunc) {
g.handle(http.MethodHead, path, h)
}
// Options adds an OPTIONS route & handler to the router.
func (g *routeGroup) Options(path string, h http.HandlerFunc) {
g.handle(http.MethodOptions, path, h)
}
// Patch adds a PATCH route & handler to the router.
func (g *routeGroup) Patch(path string, h http.HandlerFunc) {
g.handle(http.MethodPatch, path, h)
}
// Post adds a POST route & handler to the router.
func (g *routeGroup) Post(path string, h http.HandlerFunc) {
g.handle(http.MethodPost, path, h)
}
// Put adds a PUT route & handler to the router.
func (g *routeGroup) Put(path string, h http.HandlerFunc) {
g.handle(http.MethodPut, path, h)
}
// Trace adds a TRACE route & handler to the router.
func (g *routeGroup) Trace(path string, h http.HandlerFunc) {
g.handle(http.MethodTrace, path, h)
}
// Handle allows for any method to be registered with the given
// route & handler. Allows for non standard methods to be used
// like CalDavs PROPFIND and so forth.
func (g *routeGroup) Handle(method string, path string, h http.HandlerFunc) {
g.handle(method, path, h)
}
// Any adds a route & handler to the router for all HTTP methods.
func (g *routeGroup) Any(path string, h http.HandlerFunc) {
g.Connect(path, h)
g.Delete(path, h)
g.Get(path, h)
g.Head(path, h)
g.Options(path, h)
g.Patch(path, h)
g.Post(path, h)
g.Put(path, h)
g.Trace(path, h)
}
// Match adds a route & handler to the router for multiple HTTP methods provided.
func (g *routeGroup) Match(methods []string, path string, h http.HandlerFunc) {
for _, m := range methods {
g.handle(m, path, h)
}
}
// GroupWithNone creates a new sub router with specified prefix and no middleware attached.
func (g *routeGroup) GroupWithNone(prefix string) IRouteGroup {
return &routeGroup{
prefix: g.prefix + prefix,
pure: g.pure,
middleware: make([]Middleware, 0),
}
}
// GroupWithMore creates a new sub router with specified prefix, retains existing middleware and adds new middleware.
func (g *routeGroup) GroupWithMore(prefix string, middleware ...Middleware) IRouteGroup {
rg := &routeGroup{
prefix: g.prefix + prefix,
pure: g.pure,
middleware: make([]Middleware, len(g.middleware)),
}
copy(rg.middleware, g.middleware)
rg.Use(middleware...)
return rg
}
// Group creates a new sub router with specified prefix and retains existing middleware.
func (g *routeGroup) Group(prefix string) IRouteGroup {
rg := &routeGroup{
prefix: g.prefix + prefix,
pure: g.pure,
middleware: make([]Middleware, len(g.middleware)),
}
copy(rg.middleware, g.middleware)
return rg
}