-
Notifications
You must be signed in to change notification settings - Fork 26
/
router.go
187 lines (159 loc) · 4.78 KB
/
router.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
package yee
import (
"embed"
"io/fs"
"net/http"
"path"
"strings"
)
type Router struct {
handlers []HandlerFunc
core *Core
root bool
basePath string
}
// RestfulAPI is the default implementation of restfulApi interface
type RestfulAPI struct {
Get HandlerFunc
Post HandlerFunc
Delete HandlerFunc
Put HandlerFunc
}
// Implement the HTTP method and add to the router table
// GET,POST,PUT,DELETE,OPTIONS,TRACE,HEAD,PATCH
// these are defined in RFC 7231 section 4.3.
func (r *Router) GET(path string, handler ...HandlerFunc) {
r.handle(http.MethodGet, path, handler)
}
func (r *Router) POST(path string, handler ...HandlerFunc) {
r.handle(http.MethodPost, path, handler)
}
func (r *Router) PUT(path string, handler ...HandlerFunc) {
r.handle(http.MethodPut, path, handler)
}
func (r *Router) DELETE(path string, handler ...HandlerFunc) {
r.handle(http.MethodDelete, path, handler)
}
func (r *Router) PATCH(path string, handler ...HandlerFunc) {
r.handle(http.MethodPatch, path, handler)
}
func (r *Router) HEAD(path string, handler ...HandlerFunc) {
r.handle(http.MethodHead, path, handler)
}
func (r *Router) TRACE(path string, handler ...HandlerFunc) {
r.handle(http.MethodTrace, path, handler)
}
func (r *Router) OPTIONS(path string, handler ...HandlerFunc) {
r.handle(http.MethodOptions, path, handler)
}
func (r *Router) Restful(path string, api RestfulAPI) {
if api.Get != nil {
r.handle(http.MethodGet, path, HandlersChain{api.Get})
}
if api.Post != nil {
r.handle(http.MethodPost, path, HandlersChain{api.Post})
}
if api.Put != nil {
r.handle(http.MethodPut, path, HandlersChain{api.Put})
}
if api.Delete != nil {
r.handle(http.MethodDelete, path, HandlersChain{api.Delete})
}
}
func (r *Router) Any(path string, handler ...HandlerFunc) {
r.handle(http.MethodPost, path, handler)
r.handle(http.MethodGet, path, handler)
r.handle(http.MethodPut, path, handler)
r.handle(http.MethodDelete, path, handler)
r.handle(http.MethodOptions, path, handler)
}
func (r *Router) Use(middleware ...HandlerFunc) {
r.handlers = append(r.handlers, middleware...)
}
func (r *Router) Group(prefix string, handlers ...HandlerFunc) *Router {
rx := &Router{
handlers: r.combineHandlers(handlers),
core: r.core,
basePath: r.calculateAbsolutePath(prefix),
}
return rx
}
func (r *Router) handle(method, path string, handlers HandlersChain) {
absolutePath := r.calculateAbsolutePath(path)
handlers = r.combineHandlers(handlers)
r.core.addRoute(method, absolutePath, handlers)
}
func (c *Core) addRoute(method, prefix string, handlers HandlersChain) {
if prefix[0] != '/' {
panic("path must begin with '/'")
}
if method == "" {
panic("HTTP method can not be empty")
}
root := c.trees.get(method)
if root == nil {
root = new(node)
root.fullPath = "/"
c.trees = append(c.trees, methodTree{method: method, root: root})
}
root.addRoute(prefix, handlers)
// Update maxParams
if paramsCount := countParams(prefix); paramsCount > c.maxParams {
c.maxParams = paramsCount
}
}
func (r *Router) Static(relativePath, root string) {
if strings.Contains(relativePath, ":") || strings.Contains(relativePath, "*") {
panic("URL path cannot be used when serving a static folder")
}
handler := r.createDistHandler(relativePath, Dir(root, false))
url := path.Join(relativePath, "/*filepath")
r.GET(url, handler)
r.HEAD(url, handler)
}
func (r *Router) Pack(relativePath string, f embed.FS, root string) {
if strings.Contains(relativePath, ":") || strings.Contains(relativePath, "*") {
panic("URL path cannot be used when serving a static folder")
}
fsys, err := fs.Sub(f, root)
if err != nil {
panic(err)
}
handler := r.createDistHandler(relativePath, http.FS(fsys))
url := path.Join(relativePath, "/*filepath")
r.GET(url, handler)
r.HEAD(url, handler)
}
func (r *Router) createDistHandler(relativePath string, fs http.FileSystem) HandlerFunc {
absolutePath := r.calculateAbsolutePath(relativePath)
fileServer := http.StripPrefix(absolutePath, http.FileServer(fs))
return func(c Context) (err error) {
if _, noListing := fs.(*onlyFilesFS); noListing {
c.Response().WriteHeader(http.StatusNotFound)
}
file := c.Params("filepath")
f, err2 := fs.Open(file)
if err2 != nil {
c.Status(http.StatusNotFound)
c.Reset()
}
if f != nil {
_ = f.Close()
}
fileServer.ServeHTTP(c.Response(), c.Request())
return
}
}
func (r *Router) calculateAbsolutePath(relativePath string) string {
return joinPaths(r.basePath, relativePath)
}
func (r *Router) combineHandlers(handlers HandlersChain) HandlersChain {
finalSize := len(r.handlers) + len(handlers)
if finalSize >= crashIndex {
panic("too many handlers")
}
mergedHandlers := make(HandlersChain, finalSize)
copy(mergedHandlers, r.handlers)
copy(mergedHandlers[len(r.handlers):], handlers)
return mergedHandlers
}