resource extra handler
This commit is contained in:
36
resource.go
36
resource.go
@@ -102,10 +102,38 @@ func (res *Resource) Delete(h http.HandlerFunc) {
|
||||
res.handlerFunc(http.MethodDelete, p, h)
|
||||
}
|
||||
|
||||
func (res *Resource) Handle(pattern string, h http.HandlerFunc) {
|
||||
p := suffixIt(res.pattern, "{id}")
|
||||
res.routes.Add(http.MethodDelete + " " + p)
|
||||
res.handlerFunc(http.MethodDelete, p, h)
|
||||
// HandleGET on /group-pattern/:id/pattern
|
||||
func (res *Resource) HandleGET(pattern string, h http.HandlerFunc) {
|
||||
res.handle(http.MethodGet, pattern, h)
|
||||
}
|
||||
|
||||
// HandlePOST on /group-pattern/:id/pattern
|
||||
func (res *Resource) HandlePOST(pattern string, h http.HandlerFunc) {
|
||||
res.handle(http.MethodPost, pattern, h)
|
||||
}
|
||||
|
||||
// HandlePUT on /group-pattern/:id/pattern
|
||||
func (res *Resource) HandlePUT(pattern string, h http.HandlerFunc) {
|
||||
res.handle(http.MethodPut, pattern, h)
|
||||
}
|
||||
|
||||
// HandlePATCH on /group-pattern/:id/pattern
|
||||
func (res *Resource) HandlePATCH(pattern string, h http.HandlerFunc) {
|
||||
res.handle(http.MethodPatch, pattern, h)
|
||||
}
|
||||
|
||||
// HandleDELETE on /group-pattern/:id/pattern
|
||||
func (res *Resource) HandleDELETE(pattern string, h http.HandlerFunc) {
|
||||
res.handle(http.MethodDelete, pattern, h)
|
||||
}
|
||||
|
||||
func (res *Resource) handle(method string, pattern string, h http.HandlerFunc) {
|
||||
if !strings.HasPrefix(pattern, "/") {
|
||||
pattern = "/" + pattern
|
||||
}
|
||||
p := suffixIt(res.pattern, "{id}"+pattern)
|
||||
res.routes.Add(method + " " + p)
|
||||
res.handlerFunc(method, p, h)
|
||||
}
|
||||
|
||||
// handlerFunc registers the handler function for the given pattern.
|
||||
|
Reference in New Issue
Block a user