-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.go
335 lines (271 loc) · 8.13 KB
/
rest.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package web
import (
"encoding/json"
"fmt"
"github.com/gorilla/schema"
"net/http"
"strings"
)
// Go 1.6 compat
const (
StatusUnprocessableEntity = 422 // RFC 4918, 11.2
)
type Error struct {
Status int
Err error
}
func (err Error) Error() string {
if err.Err == nil {
return fmt.Sprintf("HTTP %d", err.Status)
} else {
return fmt.Sprintf("%v", err.Err)
}
}
func Errorf(status int, f string, args ...interface{}) Error {
return Error{status, fmt.Errorf(f, args...)}
}
func RequestError(err error) Error {
return Error{StatusUnprocessableEntity, err}
}
func RequestErrorf(f string, args ...interface{}) Error {
return Errorf(StatusUnprocessableEntity, f, args...)
}
func readRequest(request *http.Request, resource IntoResource) error {
var contentType = request.Header.Get("Content-Type")
var object = resource.IntoREST()
switch contentType {
case "application/x-www-form-urlencoded":
if err := request.ParseForm(); err != nil {
return RequestError(err)
} else if err := schema.NewDecoder().Decode(object, request.PostForm); err != nil {
return RequestError(err)
}
case "application/json":
if err := json.NewDecoder(request.Body).Decode(object); err != nil {
return RequestError(err)
}
default:
return Errorf(http.StatusUnsupportedMediaType, "Unknown Content-Type: %v", contentType)
}
log.Debugf("Decode %v request for %T => %T: %#v", contentType, resource, object, object)
return nil
}
func readQuery(request *http.Request, resource QueryResource) error {
var decoder = schema.NewDecoder()
var obj = resource.QueryREST()
decoder.IgnoreUnknownKeys(true)
if err := decoder.Decode(obj, request.URL.Query()); err != nil {
return RequestError(fmt.Errorf("Decode query for %T => %T: %v", resource, obj, err))
} else {
log.Debugf("Decode query for %T => %T: %#v", resource, obj, obj)
return nil
}
}
func writeResponse(responseWriter http.ResponseWriter, object interface{}) error {
responseWriter.Header().Set("Content-Type", "application/json")
return json.NewEncoder(responseWriter).Encode(object)
}
// Encodable resource
type Resource interface{}
// Resource collection with sub-Resources
type IndexResource interface {
// TODO: List() ([]Resource, error)
Index(name string) (Resource, error)
}
// Resoruce that decodes ?... query vars ussing github.com/gorilla/schema
type QueryResource interface {
// Return object to unmarshal query params into
QueryREST() interface{}
}
// Resoruce that decodes request body vars using either encoding/json or github.com/gorilla/schema
type IntoResource interface {
// Return object to unmarshal query params into
IntoREST() interface{}
}
// Resource that supports GET
type GetResource interface {
// Return marshalable response resource
// Perform any independent post-processing + JSON encoding in the request handler goroutine.
// Must be goroutine-safe!
GetREST() (Resource, error)
}
// Resource that supports POST
type PostResource interface {
IntoResource
// Return marshalable response resource
PostREST() (Resource, error)
}
// Resource that supports PUT
type PutResource interface {
IntoResource
// Return marshalable response resource
PutREST() (Resource, error)
}
// Resource that supports DELETE
type DeleteResource interface {
// Return marshalable response resource
DeleteREST() (Resource, error)
}
type GetPostResource interface {
GetResource
PostResource
}
// Resources that are notified after POST
// Called recursively for any indexed resources in reverse order
type MutableResource interface {
ApplyREST() error
}
type API struct {
root Resource
}
func MakeAPI(root Resource) API {
return API{
root: root,
}
}
func (api API) lookup(r *http.Request) (Resource, []MutableResource, error) {
var path = r.URL.Path
// normalize path (per http.StripPrefix behavior)
if path[0] == '/' {
path = path[1:]
}
// lookup from root
var resource = api.root
var mutables []MutableResource
if mutableResource, ok := resource.(MutableResource); ok {
mutables = append(mutables, mutableResource)
}
for _, name := range strings.Split(path, "/") {
if queryResource, ok := resource.(QueryResource); !ok {
} else if err := readQuery(r, queryResource); err != nil {
return resource, nil, err
}
if indexResource, ok := resource.(IndexResource); !ok {
return resource, nil, Error{http.StatusNotFound, nil}
} else if nextResource, err := indexResource.Index(name); err != nil {
return resource, nil, err
} else if nextResource == nil {
return nil, nil, Error{http.StatusNotFound, nil}
} else {
resource = nextResource
}
if mutableResource, ok := resource.(MutableResource); ok {
mutables = append(mutables, mutableResource)
}
}
if queryResource, ok := resource.(QueryResource); !ok {
} else if err := readQuery(r, queryResource); err != nil {
return resource, nil, err
}
// reverse
for i, j := 0, len(mutables)-1; i < j; i, j = i+1, j-1 {
mutables[i], mutables[j] = mutables[j], mutables[i]
}
return resource, mutables, nil
}
func (api API) apply(resource MutableResource, parents []MutableResource) error {
if resource != nil {
if err := resource.ApplyREST(); err != nil {
return err
}
}
for _, resource := range parents {
if err := resource.ApplyREST(); err != nil {
return err
}
}
return nil
}
func (api API) handle(w http.ResponseWriter, r *http.Request) error {
resource, mutableResources, err := api.lookup(r)
if err != nil {
return err
}
switch r.Method {
case "GET":
// resolve GET resource
if getResource, ok := resource.(GetResource); !ok {
log.Warnf("Not a GetResource: %T", resource)
return Error{http.StatusMethodNotAllowed, nil}
} else if ret, err := getResource.GetREST(); err != nil {
return err
} else if ret == nil {
return Error{http.StatusNotFound, nil}
} else {
resource = ret
}
case "POST":
if postResource, ok := resource.(PostResource); !ok {
log.Warnf("Not a PostResource: %T", resource)
return Error{http.StatusMethodNotAllowed, nil}
} else if err := readRequest(r, postResource); err != nil {
return err
} else if ret, err := postResource.PostREST(); err != nil {
return err
} else if ret == nil {
return Error{http.StatusNoContent, nil}
} else {
resource = ret
}
// apply
mutableResource, _ := resource.(MutableResource)
if err := api.apply(mutableResource, mutableResources); err != nil {
return err
}
case "PUT":
if putResource, ok := resource.(PutResource); !ok {
log.Warnf("Not a PutResource: %T", resource)
return Error{http.StatusMethodNotAllowed, nil}
} else if err := readRequest(r, putResource); err != nil {
return err
} else if ret, err := putResource.PutREST(); err != nil {
return err
} else if ret == nil {
return Error{http.StatusNotFound, nil}
} else {
resource = ret
}
// apply
mutableResource, _ := resource.(MutableResource)
if err := api.apply(mutableResource, mutableResources); err != nil {
return err
}
case "DELETE":
if deleteResource, ok := resource.(DeleteResource); !ok {
log.Warnf("Not a DeleteResource: %T", resource)
return Error{http.StatusMethodNotAllowed, nil}
} else if ret, err := deleteResource.DeleteREST(); err != nil {
return err
} else if ret == nil {
return Error{http.StatusNoContent, nil}
} else {
resource = ret
}
// apply
mutableResource, _ := resource.(MutableResource)
if err := api.apply(mutableResource, mutableResources); err != nil {
return err
}
default:
return Error{http.StatusNotImplemented, nil}
}
if err := writeResponse(w, resource); err != nil {
return err
} else {
log.Infof("%v %v: %T", r.Method, r.URL.Path, resource)
}
return nil
}
func (api API) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err := api.handle(w, r); err == nil {
} else if httpError, ok := err.(Error); !ok {
log.Infof("%v %v: HTTP %v: %v", r.Method, r.URL.Path, 500, err.Error())
http.Error(w, err.Error(), 500)
} else if httpError.Err != nil {
log.Infof("%v %v: HTTP %v: %v", r.Method, r.URL.Path, httpError.Status, httpError.Err.Error())
http.Error(w, httpError.Err.Error(), httpError.Status)
} else {
log.Infof("%v %v: HTTP %v", r.Method, r.URL.Path, httpError.Status)
http.Error(w, "", httpError.Status)
}
}