Skip to content

Commit 3e590a4

Browse files
author
Sergio Andres Virviescas Santana
committed
Fix typo
1 parent e06e8fa commit 3e590a4

File tree

3 files changed

+23
-51
lines changed

3 files changed

+23
-51
lines changed

group.go

+12-36
Original file line numberDiff line numberDiff line change
@@ -5,67 +5,49 @@ import "github.com/valyala/fasthttp"
55
// Group returns a new group.
66
// Path auto-correction, including trailing slashes, is enabled by default.
77
func (g *Group) Group(path string) *Group {
8-
path = g.beginPath + path
9-
10-
return g.router.Group(path)
8+
return g.router.Group(g.prefix + path)
119
}
1210

1311
// GET is a shortcut for group.Handle(fasthttp.MethodGet, path, handler)
1412
func (g *Group) GET(path string, handler fasthttp.RequestHandler) {
15-
path = g.beginPath + path
16-
17-
g.router.GET(path, handler)
13+
g.router.GET(g.prefix+path, handler)
1814
}
1915

2016
// HEAD is a shortcut for group.Handle(fasthttp.MethodHead, path, handler)
2117
func (g *Group) HEAD(path string, handler fasthttp.RequestHandler) {
22-
path = g.beginPath + path
23-
24-
g.router.HEAD(path, handler)
18+
g.router.HEAD(g.prefix+path, handler)
2519
}
2620

2721
// OPTIONS is a shortcut for group.Handle(fasthttp.MethodOptions, path, handler)
2822
func (g *Group) OPTIONS(path string, handler fasthttp.RequestHandler) {
29-
path = g.beginPath + path
30-
31-
g.router.OPTIONS(path, handler)
23+
g.router.OPTIONS(g.prefix+path, handler)
3224
}
3325

3426
// POST is a shortcut for group.Handle(fasthttp.MethodPost, path, handler)
3527
func (g *Group) POST(path string, handler fasthttp.RequestHandler) {
36-
path = g.beginPath + path
37-
38-
g.router.POST(path, handler)
28+
g.router.POST(g.prefix+path, handler)
3929
}
4030

4131
// PUT is a shortcut for group.Handle(fasthttp.MethodPut, path, handler)
4232
func (g *Group) PUT(path string, handler fasthttp.RequestHandler) {
43-
path = g.beginPath + path
44-
45-
g.router.PUT(path, handler)
33+
g.router.PUT(g.prefix+path, handler)
4634
}
4735

4836
// PATCH is a shortcut for group.Handle(fasthttp.MethodPatch, path, handler)
4937
func (g *Group) PATCH(path string, handler fasthttp.RequestHandler) {
50-
path = g.beginPath + path
51-
52-
g.router.PATCH(path, handler)
38+
g.router.PATCH(g.prefix+path, handler)
5339
}
5440

5541
// DELETE is a shortcut for group.Handle(fasthttp.MethodDelete, path, handler)
5642
func (g *Group) DELETE(path string, handler fasthttp.RequestHandler) {
57-
path = g.beginPath + path
58-
59-
g.router.DELETE(path, handler)
43+
g.router.DELETE(g.prefix+path, handler)
6044
}
6145

6246
// ANY is a shortcut for group.Handle(router.MethodWild, path, handler)
6347
//
6448
// WARNING: Use only for routes where the request method is not important
6549
func (g *Group) ANY(path string, handler fasthttp.RequestHandler) {
66-
path = g.beginPath + path
67-
68-
g.router.ANY(path, handler)
50+
g.router.ANY(g.prefix+path, handler)
6951
}
7052

7153
// ServeFiles serves files from the given file system root.
@@ -77,9 +59,7 @@ func (g *Group) ANY(path string, handler fasthttp.RequestHandler) {
7759
// Use:
7860
// router.ServeFiles("/src/{filepath:*}", "./")
7961
func (g *Group) ServeFiles(path string, rootPath string) {
80-
path = g.beginPath + path
81-
82-
g.router.ServeFiles(path, rootPath)
62+
g.router.ServeFiles(g.prefix+path, rootPath)
8363
}
8464

8565
// ServeFilesCustom serves files from the given file system settings.
@@ -92,9 +72,7 @@ func (g *Group) ServeFiles(path string, rootPath string) {
9272
// Use:
9373
// router.ServeFilesCustom("/src/{filepath:*}", *customFS)
9474
func (g *Group) ServeFilesCustom(path string, fs *fasthttp.FS) {
95-
path = g.beginPath + path
96-
97-
g.router.ServeFilesCustom(path, fs)
75+
g.router.ServeFilesCustom(g.prefix+path, fs)
9876
}
9977

10078
// Handle registers a new request handler with the given path and method.
@@ -106,7 +84,5 @@ func (g *Group) ServeFilesCustom(path string, fs *fasthttp.FS) {
10684
// frequently used, non-standardized or custom methods (e.g. for internal
10785
// communication with a proxy).
10886
func (g *Group) Handle(method, path string, handler fasthttp.RequestHandler) {
109-
path = g.beginPath + path
110-
111-
g.router.Handle(method, path, handler)
87+
g.router.Handle(method, g.prefix+path, handler)
11288
}

router.go

+9-13
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func New() *Router {
3939
// Path auto-correction, including trailing slashes, is enabled by default.
4040
func (r *Router) Group(path string) *Group {
4141
return &Group{
42-
router: r,
43-
beginPath: path,
42+
router: r,
43+
prefix: path,
4444
}
4545
}
4646

@@ -116,20 +116,16 @@ func (r *Router) ANY(path string, handler fasthttp.RequestHandler) {
116116
// path /defined/root/dir/{filepath:*}.
117117
// For example if root is "/etc" and {filepath:*} is "passwd", the local file
118118
// "/etc/passwd" would be served.
119-
// Internally a fasthttp.FSHandler is used, therefore http.NotFound is used instead
119+
// Internally a fasthttp.FSHandler is used, therefore fasthttp.NotFound is used instead
120120
// Use:
121121
// router.ServeFiles("/src/{filepath:*}", "./")
122122
func (r *Router) ServeFiles(path string, rootPath string) {
123-
suffix := "/{filepath:*}"
124-
125-
if !strings.HasSuffix(path, suffix) {
126-
panic("path must end with " + suffix + " in path '" + path + "'")
127-
}
128-
129-
prefix := path[:len(path)-len(suffix)]
130-
fileHandler := fasthttp.FSHandler(rootPath, strings.Count(prefix, "/"))
131-
132-
r.GET(path, fileHandler)
123+
r.ServeFilesCustom(path, &fasthttp.FS{
124+
Root: rootPath,
125+
IndexNames: []string{"index.html"},
126+
GenerateIndexPages: true,
127+
AcceptByteRange: true,
128+
})
133129
}
134130

135131
// ServeFilesCustom serves files from the given file system settings.

types.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,6 @@ type Router struct {
7878

7979
// Group is a sub-router to group paths
8080
type Group struct {
81-
router *Router
82-
beginPath string
81+
router *Router
82+
prefix string
8383
}

0 commit comments

Comments
 (0)