/
router.go
143 lines (121 loc) · 4.89 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
package router
import (
"github.com/fasthttp/router"
"github.com/fushiliang321/go-core/router/types"
"github.com/fushiliang321/go-core/server/dispatch"
"github.com/valyala/fasthttp"
)
type Router struct {
router *router.Router
dispatch func(handler types.RequestHandler) fasthttp.RequestHandler
}
func New() *Router {
return &Router{
router: router.New(),
dispatch: dispatch.Dispatch,
}
}
func (r *Router) Dispatch(dispatch func(handler types.RequestHandler) fasthttp.RequestHandler) {
r.dispatch = dispatch
}
func (r *Router) Group(path string) *Group {
return &Group{
group: r.router.Group(path),
}
}
func (r *Router) Mutable(v bool) {
r.router.Mutable(v)
}
func (r *Router) List() map[string][]string {
return r.router.List()
}
// GET is a shortcut for router.Handle(fasthttp.MethodGet, path, handler)
func (r *Router) GET(path string, handler types.RequestHandler) {
r.router.GET(path, r.dispatch(handler))
}
// HEAD is a shortcut for router.Handle(fasthttp.MethodHead, path, handler)
func (r *Router) HEAD(path string, handler types.RequestHandler) {
r.router.HEAD(path, r.dispatch(handler))
}
// POST is a shortcut for router.Handle(fasthttp.MethodPost, path, handler)
func (r *Router) POST(path string, handler types.RequestHandler) {
r.router.POST(path, r.dispatch(handler))
}
// PUT is a shortcut for router.Handle(fasthttp.MethodPut, path, handler)
func (r *Router) PUT(path string, handler types.RequestHandler) {
r.router.PUT(path, r.dispatch(handler))
}
// PATCH is a shortcut for router.Handle(fasthttp.MethodPatch, path, handler)
func (r *Router) PATCH(path string, handler types.RequestHandler) {
r.router.PATCH(path, r.dispatch(handler))
}
// DELETE is a shortcut for router.Handle(fasthttp.MethodDelete, path, handler)
func (r *Router) DELETE(path string, handler types.RequestHandler) {
r.router.DELETE(path, r.dispatch(handler))
}
// CONNECT is a shortcut for router.Handle(fasthttp.MethodConnect, path, handler)
func (r *Router) CONNECT(path string, handler types.RequestHandler) {
r.router.CONNECT(path, r.dispatch(handler))
}
// OPTIONS is a shortcut for router.Handle(fasthttp.MethodOptions, path, handler)
func (r *Router) OPTIONS(path string, handler types.RequestHandler) {
r.router.OPTIONS(path, r.dispatch(handler))
}
// TRACE is a shortcut for router.Handle(fasthttp.MethodTrace, path, handler)
func (r *Router) TRACE(path string, handler types.RequestHandler) {
r.router.TRACE(path, r.dispatch(handler))
}
// ANY is a shortcut for router.Handle(router.MethodWild, path, handler)
//
// WARNING: Use only for routes where the request method is not important
func (r *Router) ANY(path string, handler types.RequestHandler) {
r.router.ANY(path, r.dispatch(handler))
}
// ServeFiles serves files from the given file system root.
// The path must end with "/{filepath:*}", files are then served from the local
// path /defined/root/dir/{filepath:*}.
// For example if root is "/etc" and {filepath:*} is "passwd", the local file
// "/etc/passwd" would be served.
// Internally a fasthttp.FSHandler is used, therefore fasthttp.NotFound is used instead
// Use:
//
// router.ServeFiles("/src/{filepath:*}", "./")
func (r *Router) ServeFiles(path string, rootPath string) {
r.router.ServeFiles(path, rootPath)
}
// ServeFilesCustom serves files from the given file system settings.
// The path must end with "/{filepath:*}", files are then served from the local
// path /defined/root/dir/{filepath:*}.
// For example if root is "/etc" and {filepath:*} is "passwd", the local file
// "/etc/passwd" would be served.
// Internally a fasthttp.FSHandler is used, therefore http.NotFound is used instead
// of the Router's NotFound handler.
// Use:
//
// router.ServeFilesCustom("/src/{filepath:*}", *customFS)
func (r *Router) ServeFilesCustom(path string, fs *fasthttp.FS) {
r.router.ServeFilesCustom(path, fs)
}
// Handle registers a new request handler with the given path and method.
//
// For GET, POST, PUT, PATCH and DELETE requests the respective shortcut
// functions can be used.
//
// This function is intended for bulk loading and to allow the usage of less
// frequently used, non-standardized or custom methods (e.g. for internal
// communication with a proxy).
func (r *Router) Handle(method, path string, handler types.RequestHandler) {
r.router.Handle(method, path, r.dispatch(handler))
}
// Lookup allows the manual lookup of a method + path combo.
// This is e.g. useful to build a framework around this router.
// If the path was found, it returns the handler function.
// Otherwise the second return value indicates whether a redirection to
// the same path with an extra / without the trailing slash should be performed.
func (r *Router) Lookup(method, path string, ctx *fasthttp.RequestCtx) (fasthttp.RequestHandler, bool) {
return r.router.Lookup(method, path, ctx)
}
// Handler makes the router implement the http.Handler interface.
func (r *Router) Handler(ctx *fasthttp.RequestCtx) {
r.router.Handler(ctx)
}