forked from rs/rest-layer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
102 lines (92 loc) · 3.36 KB
/
handler.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package rest
import (
"context"
"net/http"
"github.com/rs/rest-layer/resource"
"github.com/rs/rest-layer/schema"
)
// Handler is a net/http compatible handler used to serve the configured REST API
type Handler struct {
// ResponseFormatter can be changed to extend the DefaultResponseFormatter
ResponseFormatter ResponseFormatter
// ResponseSender can be changed to extend the DefaultResponseSender
ResponseSender ResponseSender
// FallbackHandlerFunc is called when REST layer doesn't find a route for the request.
// If not set, a 404 or 405 standard REST error is returned.
FallbackHandlerFunc func(ctx context.Context, w http.ResponseWriter, r *http.Request)
// index stores the resource router
index resource.Index
}
type methodHandler func(ctx context.Context, r *http.Request, route *RouteMatch) (int, http.Header, interface{})
// NewHandler creates an new REST API HTTP handler with the specified resource index
func NewHandler(i resource.Index) (*Handler, error) {
if c, ok := i.(schema.Compiler); ok {
if err := c.Compile(); err != nil {
return nil, err
}
}
h := &Handler{
ResponseFormatter: DefaultResponseFormatter{},
ResponseSender: DefaultResponseSender{},
index: i,
}
return h, nil
}
// ServeHTTP handles requests as a http.Handler
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
h.ServeHTTPC(ctx, w, r)
}
// ServeHTTPC handles requests as a xhandler.HandlerC
func (h *Handler) ServeHTTPC(ctx context.Context, w http.ResponseWriter, r *http.Request) {
// Skip body if method is HEAD
skipBody := r.Method == "HEAD"
route, err := FindRoute(h.index, r)
if err != nil {
if h.FallbackHandlerFunc != nil {
h.FallbackHandlerFunc(ctx, w, r)
} else {
h.sendResponse(ctx, w, 0, http.Header{}, err, skipBody)
}
return
}
defer route.Release()
// Store the route and the router in the context
ctx = contextWithRoute(ctx, route)
ctx = contextWithIndex(ctx, h.index)
// Execute the main route handler
status, headers, body := routeHandler(ctx, r, route)
if headers == nil {
headers = http.Header{}
}
if h.FallbackHandlerFunc != nil && (body == errResourceNotFound || body == ErrInvalidMethod) {
h.FallbackHandlerFunc(ctx, w, r)
return
}
h.sendResponse(ctx, w, status, headers, body, skipBody)
}
// routeHandler executes the appropriate method handler for the request if allowed by the route configuration
func routeHandler(ctx context.Context, r *http.Request, route *RouteMatch) (status int, headers http.Header, body interface{}) {
// Check route's resource parent(s) exists
if err := route.ResourcePath.ParentsExist(ctx); err != nil {
return 0, http.Header{}, err
}
rsrc := route.Resource()
if rsrc == nil {
return http.StatusNotFound, nil, errResourceNotFound
}
conf := rsrc.Conf()
isItem := route.ResourceID() != nil
mh := getAllowedMethodHandler(isItem, route.Method, conf)
if mh == nil {
headers = http.Header{}
setAllowHeader(headers, isItem, conf)
return ErrInvalidMethod.Code, headers, ErrInvalidMethod
}
return mh(ctx, r, route)
}
// sendResponse format and send the API response
func (h *Handler) sendResponse(ctx context.Context, w http.ResponseWriter, status int, headers http.Header, res interface{}, skipBody bool) {
ctx, status, body := formatResponse(ctx, h.ResponseFormatter, w, status, headers, res, skipBody)
h.ResponseSender.Send(ctx, w, status, headers, body)
}