forked from mingderwang/gas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
333 lines (274 loc) · 7.74 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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package gas
import (
"github.com/buaazp/fasthttprouter"
"github.com/valyala/fasthttp"
"reflect"
"strings"
)
var supportRestProto = [7]string{"GET", "POST", "DELETE", "HEAD", "OPTIONS", "PUT", "PATCH"}
type (
// Router class include httprouter and gas
Router struct {
*fasthttprouter.Router
g *Engine
middlewares []GasMiddlewareFunc
}
// MiddlewareFunc middlewarefunc define
GasMiddlewareFunc func(GasHandler) GasHandler
// CHandler is a function type for rout handler
GasHandler func(*Context) error
// PanicHandler defined panic handler
PanicHandler func(*Context, interface{}) error
)
func newRouter(g *Engine) *Router {
fastR := fasthttprouter.New()
r := &Router{}
r.Router = fastR
r.g = g
return r
}
//func (r *Router) wrapGasHandlerToFasthttpRequestHandler(h GasHandler) fasthttp.RequestHandler {
// // type RequestHandler func(ctx *RequestCtx)
// return func(ctx *fasthttp.RequestCtx) {
// gasCtx := r.g.pool.Get().(*Context)
// gasCtx.reset(ctx, nil, r.g)
//
// // chain middleware functions
// var cpch GasHandler // copy handle avoid repeat chain
// cpch = h
//
// for i := len(r.middlewares) - 1; i >= 0; i-- {
// cpch = r.middlewares[i](cpch)
// }
//
// if err := cpch(ctx); err != nil {
// // handle error
// }
//
// if gasCtx.isUseDB {
// defer gasCtx.CloseDB()
// }
//
// // ctx.handlerFunc = ch
// // ctx.Next()
//
// r.g.pool.Put(gasCtx)
// }
//}
func (r *Router) wrapGasHandlerToFasthttpRouterHandler(h GasHandler) fasthttprouter.Handle {
// type Handle func(*fasthttp.RequestCtx, Params)
return func(ctx *fasthttp.RequestCtx, ps fasthttprouter.Params) {
gasCtx := r.g.pool.Get().(*Context)
gasCtx.reset(ctx, &ps, r.g)
// chain middleware functions
var cpch GasHandler // copy handle avoid repeat chain
cpch = h
for i := len(r.middlewares) - 1; i >= 0; i-- {
cpch = r.middlewares[i](cpch)
}
if err := cpch(gasCtx); err != nil {
// handle error
}
if gasCtx.isUseDB {
defer gasCtx.CloseDB()
}
// ctx.handlerFunc = ch
// ctx.Next()
r.g.pool.Put(gasCtx)
}
}
// SetNotFoundHandler set Notfound and Panic handler
func (r *Router) SetNotFoundHandler(h GasHandler) {
r.NotFound = func(fctx *fasthttp.RequestCtx) {
ctx := r.g.pool.Get().(*Context) //createContext(rw, req)
ctx.reset(fctx, nil, r.g)
// chain middleware functions
var cpch GasHandler // copy handle avoid repeat chain
cpch = h
for i := len(r.middlewares) - 1; i >= 0; i-- {
cpch = r.middlewares[i](cpch)
}
if err := cpch(ctx); err != nil {
}
r.g.pool.Put(ctx)
}
//r.NotFound = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
// ctx := r.g.pool.Get().(*Context) //createContext(rw, req)
// ctx.reset(w, req, r.g)
//
// // chain middleware functions
// var cpch GasHandler // copy handle avoid repeat chain
// cpch = h
//
// for i := len(r.middlewares) - 1; i >= 0; i-- {
// cpch = r.middlewares[i](cpch)
// }
//
// if err := cpch(ctx); err != nil {
//
// }
//
// r.g.pool.Put(ctx)
//})
}
func (r *Router) SetPanicHandler(ph PanicHandler) {
r.PanicHandler = func(fctx *fasthttp.RequestCtx, rcv interface{}) {
ctx := r.g.pool.Get().(*Context) //createContext(rw, req)
ctx.reset(fctx, nil, r.g)
if err := ph(ctx, rcv); err != nil {
}
r.g.pool.Put(ctx)
}
//r.hr.PanicHandler = func(w http.ResponseWriter, req *http.Request, rcv interface{}) {
// // c := a.createContext(w, req)
// // a.panicFunc(c, rcv)
// // a.pool.Put(c)
//
// ctx := r.g.pool.Get().(*Context) //createContext(rw, req)
// ctx.reset(w, req, r.g)
//
// if err := ph(ctx, rcv); err != nil {
//
// }
//
// r.g.pool.Put(ctx)
//}
}
func (r *Router) Use(m interface{}) {
m = wrapMiddleware(m)
r.middlewares = append(r.middlewares, m.(GasMiddlewareFunc))
}
// wrapMiddleware wraps middleware.
func wrapMiddleware(m interface{}) GasMiddlewareFunc {
switch m := m.(type) {
case GasMiddlewareFunc:
return m
case func(GasHandler) GasHandler:
return m
case GasHandler:
return wrapHandlerFuncToMiddlewareFunc(m)
case func(c *Context) error:
return wrapHandlerFuncToMiddlewareFunc(m)
default:
panic("unknown middleware")
}
}
func wrapHandlerFuncToMiddlewareFunc(m GasHandler) GasMiddlewareFunc {
return func(h GasHandler) GasHandler {
return func(c *Context) error {
if err := m(c); err != nil {
return err
}
return h(c)
}
}
}
func (r *Router) setRoute(method, path string, ch GasHandler) {
//r.hr.Handle(method, path, func(rw http.ResponseWriter, req *http.Request, ps httprouter.Params) {
h := r.wrapGasHandlerToFasthttpRouterHandler(ch)
r.Handle(method, path, h)
}
//func checkHandler(h interface{}) GasHandler {
//
// switch h := h.(type) {
// case GasHandler:
// return h
// case func(*Context) error:
// return h
// default:
// panic("handler type error")
// }
//}
func (r *Router) chainMiddleware(h GasHandler, middlewares ...interface{}) GasHandler {
var res GasHandler
res = h
for i := len(middlewares) - 1; i >= 0; i-- {
m := wrapMiddleware(middlewares[i])
res = m(res)
}
return res
}
func (r *Router) set(method, path string, ch GasHandler, middlewares ...interface{}) {
if len(middlewares) != 0 {
ch = r.chainMiddleware(ch, middlewares...)
}
r.setRoute(method, path, ch)
}
// Get REST funcs
func (r *Router) Get(path string, ch GasHandler, middlewares ...interface{}) {
r.set("GET", path, ch, middlewares...)
}
// Post REST funcs
func (r *Router) Post(path string, ch GasHandler, middlewares ...interface{}) {
r.set("POST", path, ch, middlewares...)
}
// Delete REST funcs
func (r *Router) Delete(path string, ch GasHandler, middlewares ...interface{}) {
r.set("DELETE", path, ch, middlewares...)
}
// Head REST funcs
func (r *Router) Head(path string, ch GasHandler, middlewares ...interface{}) {
r.set("HEAD", path, ch, middlewares...)
}
// Options REST funcs
func (r *Router) Options(path string, ch GasHandler, middlewares ...interface{}) {
r.set("OPTIONS", path, ch, middlewares...)
}
// Put REST funcs
func (r *Router) Put(path string, ch GasHandler, middlewares ...interface{}) {
r.set("PUT", path, ch, middlewares...)
}
// Patch REST funcs
func (r *Router) Patch(path string, ch GasHandler, middlewares ...interface{}) {
r.set("PATCH", path, ch, middlewares...)
}
func (r *Router) StaticPath(dir string) {
//fileServer := http.FileServer(http.Dir(dir))
//r.hr.GET("/"+dir+"/*filepath", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
// w.Header().Set("Vary", "Accept-Encoding")
// w.Header().Set("Cache-Control", "public, max-age=7776000")
// r.URL.Path = p.ByName("filepath")
// fileServer.ServeHTTP(w, r)
//})
//r.Router.ServeFiles("/"+dir+"/*filepath", dir)
path := "/" + dir + "/*filepath"
//absFilePath, _ := filepath.Abs(dir)
//println(absFilePath)
fs := &fasthttp.FS{
Root: dir,
//IndexNames: []string{"index.html"},
GenerateIndexPages: false,
Compress: true,
AcceptByteRange: true,
}
prefix := path[:len(path)-10]
stripSlashes := strings.Count(prefix, "/")
fs.PathRewrite = fasthttp.NewPathSlashesStripper(stripSlashes)
fsHandler := fs.NewRequestHandler()
r.GET(path, func(ctx *fasthttp.RequestCtx, _ fasthttprouter.Params) {
fsHandler(ctx)
})
}
// REST for set all REST route
func (r *Router) REST(path string, c ControllerInterface) {
// get all functions in controller
refT := reflect.TypeOf(c)
for i := 0; i < refT.NumMethod(); i++ {
m := refT.Method(i)
if checkSupportProto(m.Name) {
revf := reflect.ValueOf(c)
r.set(strings.ToUpper(m.Name), path, revf.MethodByName(m.Name).Interface().(func(*Context) error))
}
}
}
func checkSupportProto(proto string) bool {
for _, v := range supportRestProto {
if v == strings.ToUpper(proto) {
return true
}
}
return false
}
//func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// r.ServeHTTP(w, req)
//}