-
Notifications
You must be signed in to change notification settings - Fork 26
/
group.go
211 lines (174 loc) · 5.27 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package lars
import (
"strconv"
"strings"
"github.com/gorilla/websocket"
)
// IRouteGroup interface for router group
type IRouteGroup interface {
IRoutes
GroupWithNone(prefix string) IRouteGroup
GroupWithMore(prefix string, middleware ...Handler) IRouteGroup
Group(prefix string) IRouteGroup
}
// IRoutes interface for routes
type IRoutes interface {
Use(...Handler)
Any(string, ...Handler)
Get(string, ...Handler)
Post(string, ...Handler)
Delete(string, ...Handler)
Patch(string, ...Handler)
Put(string, ...Handler)
Options(string, ...Handler)
Head(string, ...Handler)
Connect(string, ...Handler)
Trace(string, ...Handler)
WebSocket(websocket.Upgrader, string, Handler)
}
// routeGroup struct containing all fields and methods for use.
type routeGroup struct {
prefix string
middleware HandlersChain
lars *LARS
}
var _ IRouteGroup = &routeGroup{}
func (g *routeGroup) handle(method string, path string, handlers []Handler) {
if len(handlers) == 0 {
panic("No handler mapped to path:" + path)
}
if i := strings.Index(path, "//"); i != -1 {
panic("Bad path '" + path + "' contains duplicate // at index:" + strconv.Itoa(i))
}
chain := make(HandlersChain, len(handlers))
name := ""
for i, h := range handlers {
if i == len(handlers)-1 {
chain[i], name = g.lars.wrapHandlerWithName(h)
} else {
chain[i] = g.lars.wrapHandler(h)
}
}
tree := g.lars.trees[method]
if tree == nil {
tree = new(node)
g.lars.trees[method] = tree
}
combined := make(HandlersChain, len(g.middleware)+len(chain))
copy(combined, g.middleware)
copy(combined[len(g.middleware):], chain)
pCount := tree.add(g.prefix+path, name, combined)
pCount++
if pCount > g.lars.mostParams {
g.lars.mostParams = pCount
}
}
// Use adds a middleware handler to the group middleware chain.
func (g *routeGroup) Use(m ...Handler) {
for _, h := range m {
g.middleware = append(g.middleware, g.lars.wrapHandler(h))
}
}
// Connect adds a CONNECT route & handler to the router.
func (g *routeGroup) Connect(path string, h ...Handler) {
g.handle(CONNECT, path, h)
}
// Delete adds a DELETE route & handler to the router.
func (g *routeGroup) Delete(path string, h ...Handler) {
g.handle(DELETE, path, h)
}
// Get adds a GET route & handler to the router.
func (g *routeGroup) Get(path string, h ...Handler) {
g.handle(GET, path, h)
}
// Head adds a HEAD route & handler to the router.
func (g *routeGroup) Head(path string, h ...Handler) {
g.handle(HEAD, path, h)
}
// Options adds an OPTIONS route & handler to the router.
func (g *routeGroup) Options(path string, h ...Handler) {
g.handle(OPTIONS, path, h)
}
// Patch adds a PATCH route & handler to the router.
func (g *routeGroup) Patch(path string, h ...Handler) {
g.handle(PATCH, path, h)
}
// Post adds a POST route & handler to the router.
func (g *routeGroup) Post(path string, h ...Handler) {
g.handle(POST, path, h)
}
// Put adds a PUT route & handler to the router.
func (g *routeGroup) Put(path string, h ...Handler) {
g.handle(PUT, path, h)
}
// Trace adds a TRACE route & handler to the router.
func (g *routeGroup) Trace(path string, h ...Handler) {
g.handle(TRACE, 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 ...Handler) {
g.handle(method, path, h)
}
// Any adds a route & handler to the router for all HTTP methods.
func (g *routeGroup) Any(path string, h ...Handler) {
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 ...Handler) {
for _, m := range methods {
g.handle(m, path, h)
}
}
// WebSocket adds a websocket route
func (g *routeGroup) WebSocket(upgrader websocket.Upgrader, path string, h Handler) {
handler := g.lars.wrapHandler(h)
g.Get(path, func(c Context) {
ctx := c.BaseContext()
var err error
ctx.websocket, err = upgrader.Upgrade(ctx.response, ctx.request, nil)
if err != nil {
return
}
defer ctx.websocket.Close()
c.Next()
}, handler)
}
// 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,
lars: g.lars,
middleware: make(HandlersChain, 0),
}
}
// GroupWithMore creates a new sub router with specified prefix, retains existing middleware and adds new middleware.
func (g *routeGroup) GroupWithMore(prefix string, middleware ...Handler) IRouteGroup {
rg := &routeGroup{
prefix: g.prefix + prefix,
lars: g.lars,
middleware: make(HandlersChain, 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,
lars: g.lars,
middleware: make(HandlersChain, len(g.middleware)),
}
copy(rg.middleware, g.middleware)
return rg
}