-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroute.go
237 lines (194 loc) · 4.61 KB
/
route.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package coco
import (
"io/fs"
"net/http"
fp "path"
"path/filepath"
"strings"
"github.com/julienschmidt/httprouter"
)
type allowedMethod int
const (
GET allowedMethod = 1 << iota
POST
PUT
DELETE
PATCH
OPTIONS
HEAD
)
var methods = map[allowedMethod]string{
GET: "GET",
POST: "POST",
PUT: "PUT",
DELETE: "DELETE",
PATCH: "PATCH",
OPTIONS: "OPTIONS",
HEAD: "HEAD",
}
type routePath struct {
name string
handlers []Handler
method string
}
type route struct {
base string
hr *httprouter.Router
parent *route
middleware []Handler
paths []routePath
paramHandlers map[string]ParamHandler
app *App
cachedMiddleware []Handler
rootNode bool
children map[string]*route
}
func (r *route) combineHandlers(handlers ...Handler) []Handler {
middlewares := make([]Handler, 0)
middlewares = append(middlewares, r.fetchMiddleware()...)
return append(middlewares, handlers...)
}
func (a *App) makePath(p string) string {
if p == "" {
p = "/"
}
clean := fp.Clean(p)
if clean[0] != '/' {
clean = "/" + clean
}
return a.basePath + clean
}
func (a *App) newRoute(path string, isRoot bool, parent *route) *route {
var r route
cleanPath := a.makePath(path)
if isRoot {
r = route{
base: cleanPath,
hr: a.router,
app: a,
rootNode: true,
parent: nil,
children: make(map[string]*route),
}
a.route = &r
} else {
combinedPath := filepath.Join(parent.base, cleanPath)
r = route{
base: combinedPath,
hr: a.router,
app: a,
parent: parent,
children: make(map[string]*route),
}
parent.children[combinedPath] = &r
}
return &r
}
func (r *route) getFullPath(path string) string {
var builder strings.Builder
builder.WriteString(strings.TrimSuffix(r.base, "/"))
if len(path) > 0 && path[0] != '/' {
builder.WriteRune('/')
}
builder.WriteString(path)
return builder.String()
}
func (r *route) fetchMiddleware() []Handler {
if r.rootNode {
return r.middleware
}
if r.cachedMiddleware == nil {
var middleware []Handler
for current := r; current != nil; current = current.parent {
middleware = append(current.middleware, middleware...)
}
r.cachedMiddleware = middleware
}
return r.cachedMiddleware
}
func (r *route) handle(httpMethod string, path string, handlers []Handler) {
newPath := routePath{
name: r.getFullPath(path),
handlers: handlers,
method: httpMethod,
}
if r.paths == nil {
r.paths = make([]routePath, 0)
}
r.paths = append(r.paths, newPath)
}
func execParamChain(ctx *context, params httprouter.Params, handlers map[string]ParamHandler) {
if len(handlers) == 0 {
return
}
pending := make([]Handler, 0)
for _, p := range params {
if h, ok := handlers[p.Key]; ok {
fn := func(rw Response, req *Request, next NextFunc) {
h(rw, req, next, p.Value)
}
pending = append(pending, fn)
}
}
ctx.handlers = append(pending, ctx.handlers...)
}
func (r *route) Path() string {
return r.base
}
func (r *route) NewRouter(path string) *route {
return r.app.newRoute(path, false, r)
}
func (r *route) Use(middleware ...Handler) *route {
r.middleware = append(r.middleware, middleware...)
return r
}
func (r *route) Get(path string, handlers ...Handler) *route {
r.handle("GET", path, handlers)
return r
}
func (r *route) Post(path string, handlers ...Handler) *route {
r.handle("POST", path, handlers)
return r
}
func (r *route) Put(path string, handlers ...Handler) *route {
r.handle("PUT", path, handlers)
return r
}
func (r *route) Delete(path string, handlers ...Handler) *route {
r.handle("DELETE", path, handlers)
return r
}
func (r *route) Patch(path string, handlers ...Handler) *route {
r.handle("PATCH", path, handlers)
return r
}
func (r *route) Options(path string, handlers ...Handler) *route {
r.handle("OPTIONS", path, handlers)
return r
}
func (r *route) Head(path string, handlers ...Handler) *route {
r.handle("HEAD", path, handlers)
return r
}
func (r *route) All(path string, handlers ...Handler) *route {
for _, v := range methods {
r.handle(v, path, handlers)
}
return r
}
func (r *route) Static(root fs.FS, path string) *route {
strippedPath := "/" + strings.Trim(path, "/")
fileServer := http.FileServer(http.FS(root))
r.hr.GET(strippedPath+"/*filepath", func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
req.URL.Path = ps.ByName("filepath")
fileServer.ServeHTTP(w, req)
})
return r
}
func (r *route) Param(param string, handler ParamHandler) *route {
if r.paramHandlers == nil {
r.paramHandlers = make(map[string]ParamHandler)
}
r.paramHandlers[param] = handler
return r
}