feat: `Handles` method supports setting multiple http methods for a request handle at the same time

This commit is contained in:
AH-dark 2022-08-23 12:52:25 +08:00
parent 1c48977cca
commit 4ec29dc2d9
No known key found for this signature in database
GPG Key ID: 73D3D212EB75AB80
1 changed files with 13 additions and 0 deletions

View File

@ -106,6 +106,19 @@ func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers ...Ha
return group.handle(httpMethod, relativePath, handlers) 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). // POST is a shortcut for router.Handle("POST", path, handle).
func (group *RouterGroup) POST(relativePath string, handlers ...HandlerFunc) IRoutes { func (group *RouterGroup) POST(relativePath string, handlers ...HandlerFunc) IRoutes {
return group.handle(http.MethodPost, relativePath, handlers) return group.handle(http.MethodPost, relativePath, handlers)