From 4ec29dc2d9941c821399d246f3ac479f74fe1ad3 Mon Sep 17 00:00:00 2001 From: AH-dark Date: Tue, 23 Aug 2022 12:52:25 +0800 Subject: [PATCH 1/3] feat: `Handles` method supports setting multiple http methods for a request handle at the same time --- routergroup.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/routergroup.go b/routergroup.go index 2474a81c..93c8e054 100644 --- a/routergroup.go +++ b/routergroup.go @@ -106,6 +106,19 @@ func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers ...Ha return group.handle(httpMethod, relativePath, handlers) } +// Handles registers multiple new request handle with the given path and method. +// Its specific usage is the same as the Handle method, but it supports setting multiple http methods at the same time. +func (group *RouterGroup) Handles(httpMethods []string, relativePath string, handlers ...HandlerFunc) IRoutes { + for _, method := range httpMethods { + if matched := regEnLetter.MatchString(method); !matched { + panic("http method " + method + " is not valid") + } + group.handle(method, relativePath, handlers) + } + + return group.returnObj() +} + // POST is a shortcut for router.Handle("POST", path, handle). func (group *RouterGroup) POST(relativePath string, handlers ...HandlerFunc) IRoutes { return group.handle(http.MethodPost, relativePath, handlers) From 1c1da49bb164973c2b84eeb215a1c0228126970b Mon Sep 17 00:00:00 2001 From: AH-dark Date: Tue, 23 Aug 2022 12:54:50 +0800 Subject: [PATCH 2/3] fix: added the declaration of the method to the interface. --- routergroup.go | 1 + 1 file changed, 1 insertion(+) diff --git a/routergroup.go b/routergroup.go index 93c8e054..ee28d618 100644 --- a/routergroup.go +++ b/routergroup.go @@ -34,6 +34,7 @@ type IRoutes interface { Use(...HandlerFunc) IRoutes Handle(string, string, ...HandlerFunc) IRoutes + Handles([]string, string, ...HandlerFunc) IRoutes Any(string, ...HandlerFunc) IRoutes GET(string, ...HandlerFunc) IRoutes POST(string, ...HandlerFunc) IRoutes From 05736c6b1b91cc114ee380c0dc4ff93341b4556e Mon Sep 17 00:00:00 2001 From: AH-dark Date: Tue, 23 Aug 2022 14:37:17 +0800 Subject: [PATCH 3/3] test: unit test for Handlers method in RouterGroup interface --- routergroup_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/routergroup_test.go b/routergroup_test.go index 41f96372..a24ae168 100644 --- a/routergroup_test.go +++ b/routergroup_test.go @@ -178,6 +178,7 @@ func testRoutesInterface(t *testing.T, r IRoutes) { assert.Equal(t, r, r.Use(handler)) assert.Equal(t, r, r.Handle(http.MethodGet, "/handler", handler)) + assert.Equal(t, r, r.Handles([]string{http.MethodGet, http.MethodHead, http.MethodPost}, "/handlers", handler)) assert.Equal(t, r, r.Any("/any", handler)) assert.Equal(t, r, r.GET("/", handler)) assert.Equal(t, r, r.POST("/", handler))