-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutergroup.go
137 lines (116 loc) · 4.54 KB
/
routergroup.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
package tokay
import (
"strings"
"github.com/valyala/fasthttp"
)
// RouterGroup represents a group of routes that share the same path prefix.
type RouterGroup struct {
path string
engine *Engine
handlers []Handler
}
// newRouteGroup creates a new RouterGroup with the given path, engine, and handlers.
func newRouteGroup(path string, engine *Engine, handlers []Handler) *RouterGroup {
return &RouterGroup{
path: path,
engine: engine,
handlers: handlers,
}
}
// Path returns RouterGroup fullpath
func (r *RouterGroup) Path() (path string) {
return r.path
}
// GET adds a GET route to the engine with the given route path and handlers.
func (r *RouterGroup) GET(path string, handlers ...Handler) *Route {
return newRoute(path, r).GET(handlers...)
}
// POST adds a POST route to the engine with the given route path and handlers.
func (r *RouterGroup) POST(path string, handlers ...Handler) *Route {
return newRoute(path, r).POST(handlers...)
}
// PUT adds a PUT route to the engine with the given route path and handlers.
func (r *RouterGroup) PUT(path string, handlers ...Handler) *Route {
return newRoute(path, r).PUT(handlers...)
}
// PATCH adds a PATCH route to the engine with the given route path and handlers.
func (r *RouterGroup) PATCH(path string, handlers ...Handler) *Route {
return newRoute(path, r).PATCH(handlers...)
}
// DELETE adds a DELETE route to the engine with the given route path and handlers.
func (r *RouterGroup) DELETE(path string, handlers ...Handler) *Route {
return newRoute(path, r).DELETE(handlers...)
}
// CONNECT adds a CONNECT route to the engine with the given route path and handlers.
func (r *RouterGroup) CONNECT(path string, handlers ...Handler) *Route {
return newRoute(path, r).CONNECT(handlers...)
}
// HEAD adds a HEAD route to the engine with the given route path and handlers.
func (r *RouterGroup) HEAD(path string, handlers ...Handler) *Route {
return newRoute(path, r).HEAD(handlers...)
}
// OPTIONS adds an OPTIONS route to the engine with the given route path and handlers.
func (r *RouterGroup) OPTIONS(path string, handlers ...Handler) *Route {
return newRoute(path, r).OPTIONS(handlers...)
}
// TRACE adds a TRACE route to the engine with the given route path and handlers.
func (r *RouterGroup) TRACE(path string, handlers ...Handler) *Route {
return newRoute(path, r).TRACE(handlers...)
}
// Any adds a route with the given route, handlers, and the HTTP methods as listed in routing.Methods.
func (r *RouterGroup) Any(path string, handlers ...Handler) *Route {
route := newRoute(path, r)
for _, method := range Methods {
route.add(method, handlers)
}
return route
}
// To adds a route to the engine with the given HTTP methods, route path, and handlers.
// Multiple HTTP methods should be separated by commas (without any surrounding spaces).
func (r *RouterGroup) To(methods, path string, handlers ...Handler) *Route {
return newRoute(path, r).To(methods, handlers...)
}
// Group creates a RouterGroup with the given route path and handlers.
// The new group will combine the existing path with the new one.
// If no handler is provided, the new group will inherit the handlers registered
// with the current group.
func (r *RouterGroup) Group(path string, handlers ...Handler) *RouterGroup {
if len(handlers) == 0 {
handlers = make([]Handler, len(r.handlers))
copy(handlers, r.handlers)
}
if path == "" || path[0] != '/' {
path = "/" + path
}
return newRouteGroup(r.path+path, r.engine, handlers)
}
// Use registers one or multiple handlers to the current route group.
// These handlers will be shared by all routes belong to this group and its subgroups.
func (r *RouterGroup) Use(handlers ...Handler) {
r.handlers = append(r.handlers, handlers...)
}
// Static serves files from the given file system root.
// Where:
// 'path' - relative path from current engine path on site (must be without trailing slash),
// 'root' - directory that contains served files. For example:
// engine.Static("/static", "/var/www")
func (r *RouterGroup) Static(path, root string, compress ...bool) *Route {
if len(compress) == 0 {
compress = append(compress, true)
}
if path == "" || path[len(path)-1] != '/' {
path += "/"
}
group := r.Group(path)
handler := (&fasthttp.FS{
Root: root,
Compress: compress[0],
PathRewrite: func(ctx *fasthttp.RequestCtx) []byte {
url := strings.Split(string(ctx.Request.RequestURI()), "?")[0]
return []byte("/" + strings.TrimPrefix(url, group.path))
},
}).NewRequestHandler()
return newRoute("*", group).To("GET,HEAD", func(c *Context) {
handler(c.RequestCtx)
})
}