From b34a77bc9602738c0190909f7c890e53d30207eb Mon Sep 17 00:00:00 2001 From: ReneWerner87 Date: Tue, 21 Jul 2020 23:30:52 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=89=20Sync=20route=20method=20metadata?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Fenny --- app.go | 7 ++----- app_test.go | 2 +- router.go | 3 ++- utils.go | 2 -- 4 files changed, 5 insertions(+), 9 deletions(-) diff --git a/app.go b/app.go index 10f73bac..241ee022 100644 --- a/app.go +++ b/app.go @@ -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]) } diff --git a/app_test.go b/app_test.go index d1af9286..54c72596 100644 --- a/app_test.go +++ b/app_test.go @@ -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 diff --git a/router.go b/router.go index f7af6503..7c8e8dab 100644 --- a/router.go +++ b/router.go @@ -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) } diff --git a/utils.go b/utils.go index 7560f149..121885d1 100644 --- a/utils.go +++ b/utils.go @@ -262,8 +262,6 @@ func methodInt(s string) int { return 7 case MethodPatch: return 8 - case methodUse: - return 9 default: return -1 }