-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.go
134 lines (104 loc) · 3.99 KB
/
group.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
package router
import (
"net/http"
)
// Group returns a new group.
// Path auto-correction, including trailing slashes, is enabled by default.
func (g *Group) Group(path string) *Group {
validatePath(path)
if len(g.prefix) > 0 && path == "/" {
return g
}
return g.router.Group(g.prefix + path)
}
// GET is a shortcut for group.Handle(http.MethodGet, path, handler)
func (g *Group) GET(path string, handler http.HandlerFunc) {
validatePath(path)
g.router.GET(g.prefix+path, handler)
}
// HEAD is a shortcut for group.Handle(http.MethodHead, path, handler)
func (g *Group) HEAD(path string, handler http.HandlerFunc) {
validatePath(path)
g.router.HEAD(g.prefix+path, handler)
}
// POST is a shortcut for group.Handle(http.MethodPost, path, handler)
func (g *Group) POST(path string, handler http.HandlerFunc) {
validatePath(path)
g.router.POST(g.prefix+path, handler)
}
// PUT is a shortcut for group.Handle(http.MethodPut, path, handler)
func (g *Group) PUT(path string, handler http.HandlerFunc) {
validatePath(path)
g.router.PUT(g.prefix+path, handler)
}
// PATCH is a shortcut for group.Handle(http.MethodPatch, path, handler)
func (g *Group) PATCH(path string, handler http.HandlerFunc) {
validatePath(path)
g.router.PATCH(g.prefix+path, handler)
}
// DELETE is a shortcut for group.Handle(http.MethodDelete, path, handler)
func (g *Group) DELETE(path string, handler http.HandlerFunc) {
validatePath(path)
g.router.DELETE(g.prefix+path, handler)
}
// OPTIONS is a shortcut for group.Handle(http.MethodOptions, path, handler)
func (g *Group) CONNECT(path string, handler http.HandlerFunc) {
validatePath(path)
g.router.CONNECT(g.prefix+path, handler)
}
// OPTIONS is a shortcut for group.Handle(http.MethodOptions, path, handler)
func (g *Group) OPTIONS(path string, handler http.HandlerFunc) {
validatePath(path)
g.router.OPTIONS(g.prefix+path, handler)
}
// OPTIONS is a shortcut for group.Handle(http.MethodOptions, path, handler)
func (g *Group) TRACE(path string, handler http.HandlerFunc) {
validatePath(path)
g.router.TRACE(g.prefix+path, handler)
}
// ANY is a shortcut for group.Handle(router.MethodWild, path, handler)
//
// WARNING: Use only for routes where the request method is not important
func (g *Group) ANY(path string, handler http.HandlerFunc) {
validatePath(path)
g.router.ANY(g.prefix+path, 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 http.NotFound is used instead
// Use:
//
// router.ServeFiles("/src/{filepath:*}", "./")
func (g *Group) ServeFiles(path string, rootPath string) {
validatePath(path)
g.router.ServeFiles(g.prefix+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 (g *Group) ServeFilesCustom(path string, fs http.FileSystem) {
validatePath(path)
g.router.ServeFilesCustom(g.prefix+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 (g *Group) Handle(method, path string, handler http.HandlerFunc) {
validatePath(path)
g.router.Handle(method, g.prefix+path, handler)
}