1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-22 15:24:38 +00:00

Merge pull request #661 from ReneWerner87/master

💉  Sync route method metadata
This commit is contained in:
fenny 2020-07-21 23:41:27 +02:00 committed by GitHub
commit 0cbe432ee0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 5 additions and 9 deletions

7
app.go
View File

@ -415,15 +415,12 @@ func (app *App) Routes() []*Route {
stackLoop:
for r := range app.stack[m] {
// Don't duplicate USE routesCount
if app.stack[m][r].Method == methodUse {
if app.stack[m][r].use {
for i := range routes {
if routes[i].Method == methodUse && routes[i].Path == app.stack[m][r].Path {
if routes[i].use && routes[i].Path == app.stack[m][r].Path {
continue stackLoop
}
}
// Ignore HEAD routes handling GET routesCount
} else if m != methodInt(app.stack[m][r].Method) {
continue
}
routes = append(routes, app.stack[m][r])
}

View File

@ -123,7 +123,7 @@ func Test_App_Routes(t *testing.T) {
app.Get("/Get", h)
app.Head("/Head", h)
app.Post("/post", h)
utils.AssertEqual(t, 4, len(app.Routes()))
utils.AssertEqual(t, 5, len(app.Routes()))
}
// go test -v -run=^$ -bench=Benchmark_App_Routes -benchmem -count=4

View File

@ -145,7 +145,7 @@ func (app *App) register(method, pathRaw string, handlers ...Handler) Route {
// Uppercase HTTP methods
method = utils.ToUpper(method)
// Check if the HTTP method is valid unless it's USE
if methodInt(method) == -1 {
if method != methodUse && methodInt(method) == -1 {
panic(fmt.Sprintf("add: invalid http method %s\n", method))
}
// A route requires atleast one ctx handler
@ -332,6 +332,7 @@ func (app *App) addRoute(method string, route *Route) {
app.routesCount++
app.mutex.Unlock()
route.pos = app.routesCount
route.Method = method
// Add route to the stack
app.stack[m] = append(app.stack[m], route)
}

View File

@ -262,8 +262,6 @@ func methodInt(s string) int {
return 7
case MethodPatch:
return 8
case methodUse:
return 9
default:
return -1
}