-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource.go
36 lines (30 loc) · 910 Bytes
/
resource.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package chevron
import (
"context"
"net/http"
"github.com/efritz/response"
"github.com/go-nacelle/nacelle"
)
type (
// Resource represents an object or behavior accessible from a unique URL
// pattern.
Resource interface {
// Handle is invoked when the resource is requested, regardless of the
// HTTP method.
Handle(context.Context, *http.Request, nacelle.Logger) response.Response
}
resource struct {
hm handlerMap
router *router
}
)
// Handle invokes the correct handler based on HTTP method, or the router's not
// implemented handler if no handler for that method is registered.
func (r *resource) Handle(ctx context.Context, req *http.Request, logger nacelle.Logger) response.Response {
if method, ok := methodMap[req.Method]; ok {
if handler, ok := r.hm[method]; ok {
return handler(ctx, req, logger)
}
}
return r.router.notImplementedHandler(ctx, req, logger)
}