Skip to content

Commit

Permalink
Fix typo
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Andres Virviescas Santana committed Jun 15, 2020
1 parent e06e8fa commit 3e590a4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 51 deletions.
48 changes: 12 additions & 36 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,67 +5,49 @@ import "github.com/valyala/fasthttp"
// Group returns a new group.
// Path auto-correction, including trailing slashes, is enabled by default.
func (g *Group) Group(path string) *Group {
path = g.beginPath + path

return g.router.Group(path)
return g.router.Group(g.prefix + path)
}

// GET is a shortcut for group.Handle(fasthttp.MethodGet, path, handler)
func (g *Group) GET(path string, handler fasthttp.RequestHandler) {
path = g.beginPath + path

g.router.GET(path, handler)
g.router.GET(g.prefix+path, handler)
}

// HEAD is a shortcut for group.Handle(fasthttp.MethodHead, path, handler)
func (g *Group) HEAD(path string, handler fasthttp.RequestHandler) {
path = g.beginPath + path

g.router.HEAD(path, handler)
g.router.HEAD(g.prefix+path, handler)
}

// OPTIONS is a shortcut for group.Handle(fasthttp.MethodOptions, path, handler)
func (g *Group) OPTIONS(path string, handler fasthttp.RequestHandler) {
path = g.beginPath + path

g.router.OPTIONS(path, handler)
g.router.OPTIONS(g.prefix+path, handler)
}

// POST is a shortcut for group.Handle(fasthttp.MethodPost, path, handler)
func (g *Group) POST(path string, handler fasthttp.RequestHandler) {
path = g.beginPath + path

g.router.POST(path, handler)
g.router.POST(g.prefix+path, handler)
}

// PUT is a shortcut for group.Handle(fasthttp.MethodPut, path, handler)
func (g *Group) PUT(path string, handler fasthttp.RequestHandler) {
path = g.beginPath + path

g.router.PUT(path, handler)
g.router.PUT(g.prefix+path, handler)
}

// PATCH is a shortcut for group.Handle(fasthttp.MethodPatch, path, handler)
func (g *Group) PATCH(path string, handler fasthttp.RequestHandler) {
path = g.beginPath + path

g.router.PATCH(path, handler)
g.router.PATCH(g.prefix+path, handler)
}

// DELETE is a shortcut for group.Handle(fasthttp.MethodDelete, path, handler)
func (g *Group) DELETE(path string, handler fasthttp.RequestHandler) {
path = g.beginPath + path

g.router.DELETE(path, handler)
g.router.DELETE(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 fasthttp.RequestHandler) {
path = g.beginPath + path

g.router.ANY(path, handler)
g.router.ANY(g.prefix+path, handler)
}

// ServeFiles serves files from the given file system root.
Expand All @@ -77,9 +59,7 @@ func (g *Group) ANY(path string, handler fasthttp.RequestHandler) {
// Use:
// router.ServeFiles("/src/{filepath:*}", "./")
func (g *Group) ServeFiles(path string, rootPath string) {
path = g.beginPath + path

g.router.ServeFiles(path, rootPath)
g.router.ServeFiles(g.prefix+path, rootPath)
}

// ServeFilesCustom serves files from the given file system settings.
Expand All @@ -92,9 +72,7 @@ func (g *Group) ServeFiles(path string, rootPath string) {
// Use:
// router.ServeFilesCustom("/src/{filepath:*}", *customFS)
func (g *Group) ServeFilesCustom(path string, fs *fasthttp.FS) {
path = g.beginPath + path

g.router.ServeFilesCustom(path, fs)
g.router.ServeFilesCustom(g.prefix+path, fs)
}

// Handle registers a new request handler with the given path and method.
Expand All @@ -106,7 +84,5 @@ func (g *Group) ServeFilesCustom(path string, fs *fasthttp.FS) {
// frequently used, non-standardized or custom methods (e.g. for internal
// communication with a proxy).
func (g *Group) Handle(method, path string, handler fasthttp.RequestHandler) {
path = g.beginPath + path

g.router.Handle(method, path, handler)
g.router.Handle(method, g.prefix+path, handler)
}
22 changes: 9 additions & 13 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func New() *Router {
// Path auto-correction, including trailing slashes, is enabled by default.
func (r *Router) Group(path string) *Group {
return &Group{
router: r,
beginPath: path,
router: r,
prefix: path,
}
}

Expand Down Expand Up @@ -116,20 +116,16 @@ func (r *Router) ANY(path string, handler fasthttp.RequestHandler) {
// 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
// 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) {
suffix := "/{filepath:*}"

if !strings.HasSuffix(path, suffix) {
panic("path must end with " + suffix + " in path '" + path + "'")
}

prefix := path[:len(path)-len(suffix)]
fileHandler := fasthttp.FSHandler(rootPath, strings.Count(prefix, "/"))

r.GET(path, fileHandler)
r.ServeFilesCustom(path, &fasthttp.FS{
Root: rootPath,
IndexNames: []string{"index.html"},
GenerateIndexPages: true,
AcceptByteRange: true,
})
}

// ServeFilesCustom serves files from the given file system settings.
Expand Down
4 changes: 2 additions & 2 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,6 @@ type Router struct {

// Group is a sub-router to group paths
type Group struct {
router *Router
beginPath string
router *Router
prefix string
}

0 comments on commit 3e590a4

Please sign in to comment.