Skip to content

Commit

Permalink
💉 Sync route method metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
ReneWerner87 committed Jul 21, 2020
1 parent faa3035 commit 018968f
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 9 deletions.
7 changes: 2 additions & 5 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
Expand Down
2 changes: 1 addition & 1 deletion app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 0 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,6 @@ func methodInt(s string) int {
return 7
case MethodPatch:
return 8
case methodUse:
return 9
default:
return -1
}
Expand Down

0 comments on commit 018968f

Please sign in to comment.