-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_prefix.go
195 lines (165 loc) · 5.82 KB
/
router_prefix.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
package looli
import (
"net/http"
"os"
"path"
"strings"
)
var (
default404Body = "404 page not found\n"
default405Body = "405 method not allowed\n"
)
// RouterPrefix is used internally to configure router, a RouterPrefix is associated with a basePath
// and an array of handlers (middleware)
type RouterPrefix struct {
basePath string
router *Router
Middlewares []HandlerFunc
engine *Engine
allNoRoute []HandlerFunc
allNoMethod []HandlerFunc
isPrefix bool
}
// Use adds middleware to the router.
func (p *RouterPrefix) Use(middleware ...HandlerFunc) {
if len(middleware) == 0 {
panic("there must be at least one middleware")
}
p.Middlewares = append(p.Middlewares, middleware...)
}
// Use adds handlers as middleware to the router.
func (p *RouterPrefix) UseHandler(handlers ...Handler) {
var middlwares []HandlerFunc
for _, handler := range handlers {
middlwares = append(middlwares, handler.Handle)
}
p.Use(middlwares...)
}
// Get is a shortcut for router.Handle("GET", path, handle)
func (p *RouterPrefix) Get(pattern string, handlers ...HandlerFunc) {
p.Handle(http.MethodGet, pattern, handlers...)
}
// Post is a shortcut for router.Handle("Post", path, handle)
func (p *RouterPrefix) Post(pattern string, handlers ...HandlerFunc) {
p.Handle(http.MethodPost, pattern, handlers...)
}
// Put is a shortcut for router.Handle("Put", path, handle)
func (p *RouterPrefix) Put(pattern string, handlers ...HandlerFunc) {
p.Handle(http.MethodPut, pattern, handlers...)
}
// Delete is a shortcut for router.Handle("DELETE", path, handle)
func (p *RouterPrefix) Delete(pattern string, handlers ...HandlerFunc) {
p.Handle(http.MethodDelete, pattern, handlers...)
}
// Head is a shortcut for router.Handle("HEAD", path, handle)
func (p *RouterPrefix) Head(pattern string, handlers ...HandlerFunc) {
p.Handle(http.MethodHead, pattern, handlers...)
}
// Options is a shortcut for router.Handle("OPTIONS", path, handle)
func (p *RouterPrefix) Options(pattern string, handlers ...HandlerFunc) {
p.Handle(http.MethodOptions, pattern, handlers...)
}
// Patch is a shortcut for router.Handle("PATCH", path, handle)
func (p *RouterPrefix) Patch(pattern string, handlers ...HandlerFunc) {
p.Handle(http.MethodPatch, pattern, handlers...)
}
// Any registers a route that matches all the HTTP methods.
// GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE
func (p *RouterPrefix) Any(pattern string, handlers ...HandlerFunc) {
p.Handle(http.MethodGet, pattern, handlers...)
p.Handle(http.MethodPost, pattern, handlers...)
p.Handle(http.MethodPut, pattern, handlers...)
p.Handle(http.MethodDelete, pattern, handlers...)
p.Handle(http.MethodHead, pattern, handlers...)
p.Handle(http.MethodOptions, pattern, handlers...)
p.Handle(http.MethodPatch, pattern, handlers...)
p.Handle(http.MethodTrace, pattern, handlers...)
p.Handle(http.MethodConnect, pattern, handlers...)
}
// Handle registers a new request handle and middleware with the given path and method.
func (p *RouterPrefix) Handle(method, pattern string, handlers ...HandlerFunc) {
if len(handlers) == 0 {
panic("there must be at least one handler")
}
if p.basePath != "" {
pattern = p.basePath + pattern
}
if p.isPrefix {
handlers = p.combineHandlers(handlers)
}
p.router.Handle(method, pattern, handlers)
}
// StaticFile register router pattern and response file in path
func (p *RouterPrefix) StaticFile(pattern, filepath string) {
if strings.Contains(pattern, ":") || strings.Contains(pattern, "*") {
panic("URL parameters can not be used when serving a static folder")
}
handler := func(c *Context) {
c.ServeFile(filepath)
if _, err := os.Stat(filepath); os.IsNotExist(err) {
c.statusCode = http.StatusNotFound
}
}
p.Head(pattern, handler)
p.Get(pattern, handler)
}
// Static register router pattern and response file in the request url
func (p *RouterPrefix) Static(pattern, dir string) {
if strings.Contains(pattern, ":") || strings.Contains(pattern, "*") {
panic("URL parameters can not be used when serving a static folder")
}
fileServer := http.StripPrefix(pattern, http.FileServer(http.Dir(dir)))
handler := func(c *Context) {
fileServer.ServeHTTP(c.ResponseWriter, c.Request)
if _, err := os.Stat(path.Join(dir, c.Param("filepath"))); os.IsNotExist(err) {
c.statusCode = http.StatusNotFound
}
}
urlPattern := path.Join(pattern, "/*filepath")
p.Head(urlPattern, handler)
p.Get(urlPattern, handler)
}
// combine middleware and handlers for specific route
func (p *RouterPrefix) combineHandlers(handlers []HandlerFunc) []HandlerFunc {
finalSize := len(p.Middlewares) + len(handlers)
if finalSize >= int(abortIndex) {
panic("too many handlers")
}
mergedHandlers := make([]HandlerFunc, finalSize)
copyHandlers(mergedHandlers, p.Middlewares)
copyHandlers(mergedHandlers[len(p.Middlewares):], handlers)
return mergedHandlers
}
// copy handlers
func copyHandlers(dst, src []HandlerFunc) {
for index, val := range src {
dst[index] = val
}
}
// compose global middleware for all request
func (p *RouterPrefix) composeMiddlewares() func(http.ResponseWriter, *http.Request) {
return func(rw http.ResponseWriter, req *http.Request) {
context := NewContext(p, rw, req)
httpHandler := func(c *Context) {
p.router.handleRequest(c)
}
handlers := p.combineHandlers([]HandlerFunc{httpHandler})
context.handlers = handlers
context.Next()
}
}
// Prefix creates a new router prefix. You should add all the routes that have common
// middlwares or the same path prefix. For example, all the routes that use a common
// middlware could be grouped.
func (p *RouterPrefix) Prefix(basePath string) *RouterPrefix {
return &RouterPrefix{
basePath: basePath,
router: p.router,
engine: p.engine,
isPrefix: true,
}
}
func (p *RouterPrefix) handleRequest(rw http.ResponseWriter, req *http.Request) {
handler := p.composeMiddlewares()
handler(rw, req)
}