Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💉 Sync route method metadata #661

Merged
merged 1 commit into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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