forked from SpirentOrion/luddite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource.go
259 lines (229 loc) · 8.84 KB
/
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
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
package luddite
import (
"net/http"
"net/url"
"path"
"github.com/julienschmidt/httprouter"
)
// Resource is a set of REST-oriented HTTP method handlers.
//
// All resources must implement New.
//
// Singleton-style resources may implement Get and Update. For
// read-only singleton resources, Update may hardcode a 405 response.
// The Id, List, Create, and Delete methods will never be called.
//
// Collection-style resources must also implement Id. They may
// implement List, Get, Create, Update, and Delete. For read-only
// collection resources, Create, Update, and Delete may hardcode a 405
// response.
//
// Any resource may implement POST actions. These are dispatched to
// the Action method, which should switch on the action name.
//
// NB: because golang's type system sucks, any implementation must
// take care to handle interface{} types correctly. In particular, Id,
// Create, and Update must be able to handle value parameters that
// have the same concrete type as returned by New.
type Resource interface {
// New returns a new instance of the resource.
New() interface{}
// Id returns a resource's identifier as a string.
Id(value interface{}) string
// List returns an HTTP status code and a slice of resources (or error).
List(req *http.Request) (int, interface{})
// Count returns an HTTP status code and a count of resources (or error).
Count(req *http.Request) (int, interface{})
// Get returns an HTTP status code and a single resource (or error).
Get(req *http.Request, id string) (int, interface{})
// Create returns an HTTP status code and a new resource (or error).
Create(req *http.Request, value interface{}) (int, interface{})
// Update returns an HTTP status code and an updated resource (or error).
Update(req *http.Request, id string, value interface{}) (int, interface{})
// Delete returns an HTTP status code and a deleted resource (or error).
Delete(req *http.Request, id string) (int, interface{})
// Action returns an HTTP status code and a response body (or error).
Action(req *http.Request, id string, action string) (int, interface{})
}
func AddListRoute(router *httprouter.Router, basePath string, r Resource) {
router.GET(basePath, func(rw http.ResponseWriter, req *http.Request, params httprouter.Params) {
SetContextRequestProgress(req.Context(), "luddite", "Router.List", "begin")
if status, v := r.List(req); status > 0 {
SetContextRequestProgress(req.Context(), "luddite", "Router.List", "write")
WriteResponse(rw, status, v)
}
})
}
func AddCountRoute(router *httprouter.Router, basePath string, r Resource) {
router.GET(path.Join(basePath, ":id", "count"), func(rw http.ResponseWriter, req *http.Request, params httprouter.Params) {
id := params.ByName("id")
if id != "all" {
WriteResponse(rw, http.StatusNotFound, nil)
return
}
SetContextRequestProgress(req.Context(), "luddite", "Router.Count", "begin")
if status, v := r.Count(req); status > 0 {
SetContextRequestProgress(req.Context(), "luddite", "Router.Count", "write")
WriteResponse(rw, status, v)
}
})
}
func AddGetRoute(router *httprouter.Router, basePath string, withId bool, r Resource) {
var itemPath string
if withId {
itemPath = path.Join(basePath, ":id")
} else {
itemPath = basePath
}
router.GET(itemPath, func(rw http.ResponseWriter, req *http.Request, params httprouter.Params) {
id := params.ByName("id")
SetContextRequestProgress(req.Context(), "luddite", "Router.Get", "begin")
if status, v := r.Get(req, id); status > 0 {
SetContextRequestProgress(req.Context(), "luddite", "Router.Get", "write")
WriteResponse(rw, status, v)
}
})
}
func AddCreateRoute(router *httprouter.Router, basePath string, r Resource) {
router.POST(basePath, func(rw http.ResponseWriter, req *http.Request, params httprouter.Params) {
v0 := r.New()
if err := ReadRequest(req, v0); err != nil {
WriteResponse(rw, http.StatusBadRequest, err)
return
}
SetContextRequestProgress(req.Context(), "luddite", "Router.Create", "begin")
if status, v1 := r.Create(req, v0); status > 0 {
if status == http.StatusCreated {
url := url.URL{
Scheme: req.URL.Scheme,
Host: req.URL.Host,
Path: path.Join(basePath, r.Id(v1)),
}
rw.Header().Add(HeaderLocation, url.String())
}
SetContextRequestProgress(req.Context(), "luddite", "Router.Create", "write")
WriteResponse(rw, status, v1)
}
})
}
func AddUpdateRoute(router *httprouter.Router, basePath string, withId bool, r Resource) {
var itemPath string
if withId {
itemPath = path.Join(basePath, ":id")
} else {
itemPath = basePath
}
router.PUT(itemPath, func(rw http.ResponseWriter, req *http.Request, params httprouter.Params) {
id := params.ByName("id")
v0 := r.New()
if err := ReadRequest(req, v0); err != nil {
WriteResponse(rw, http.StatusBadRequest, err)
return
}
if withId && id != r.Id(v0) {
WriteResponse(rw, http.StatusBadRequest, NewError(nil, EcodeResourceIdMismatch))
return
}
SetContextRequestProgress(req.Context(), "luddite", "Router.Update", "begin")
if status, v1 := r.Update(req, id, v0); status > 0 {
SetContextRequestProgress(req.Context(), "luddite", "Router.Update", "write")
WriteResponse(rw, status, v1)
}
})
}
func AddDeleteRoute(router *httprouter.Router, basePath string, withId bool, r Resource) {
var itemPath string
if withId {
itemPath = path.Join(basePath, ":id")
} else {
itemPath = basePath
}
router.DELETE(itemPath, func(rw http.ResponseWriter, req *http.Request, params httprouter.Params) {
id := params.ByName("id")
SetContextRequestProgress(req.Context(), "luddite", "Router.Delete", "begin")
if status, v := r.Delete(req, id); status > 0 {
SetContextRequestProgress(req.Context(), "luddite", "Router.Delete", "write")
WriteResponse(rw, status, v)
}
})
}
func AddActionRoute(router *httprouter.Router, basePath string, withId bool, r Resource) {
var actionPath string
if withId {
actionPath = path.Join(basePath, ":id", ":action")
} else {
actionPath = path.Join(basePath, ":action")
}
router.POST(actionPath, func(rw http.ResponseWriter, req *http.Request, params httprouter.Params) {
var id string
if withId {
id = params.ByName("id")
}
action := params.ByName("action")
SetContextRequestProgress(req.Context(), "luddite", "Router.Action", "begin")
if status, v := r.Action(req, id, action); status > 0 {
SetContextRequestProgress(req.Context(), "luddite", "Router.Action", "write")
WriteResponse(rw, status, v)
}
})
}
// NotImplementedResource returns HTTP 501 NotImplemented for all HTTP methods.
type NotImplementedResource struct {
}
func (r *NotImplementedResource) New() interface{} {
return &NotImplementedResource{}
}
func (r *NotImplementedResource) Id(value interface{}) string {
return ""
}
func (r *NotImplementedResource) List(req *http.Request) (int, interface{}) {
return http.StatusNotImplemented, nil
}
func (r *NotImplementedResource) Count(req *http.Request) (int, interface{}) {
return http.StatusNotImplemented, nil
}
func (r *NotImplementedResource) Get(req *http.Request, id string) (int, interface{}) {
return http.StatusNotImplemented, nil
}
func (r *NotImplementedResource) Create(req *http.Request, value interface{}) (int, interface{}) {
return http.StatusNotImplemented, nil
}
func (r *NotImplementedResource) Update(req *http.Request, id string, value interface{}) (int, interface{}) {
return http.StatusNotImplemented, nil
}
func (r *NotImplementedResource) Delete(req *http.Request, id string) (int, interface{}) {
return http.StatusNotImplemented, nil
}
func (r *NotImplementedResource) Action(req *http.Request, id, action string) (int, interface{}) {
return http.StatusNotImplemented, nil
}
// MethodNotAllowedResource returns HTTP 405 MethodNotAllowed for all HTTP methods.
type MethodNotAllowedResource struct {
}
func (r *MethodNotAllowedResource) New() interface{} {
return &MethodNotAllowedResource{}
}
func (r *MethodNotAllowedResource) Id(value interface{}) string {
return ""
}
func (r *MethodNotAllowedResource) List(req *http.Request) (int, interface{}) {
return http.StatusMethodNotAllowed, nil
}
func (r *MethodNotAllowedResource) Count(req *http.Request) (int, interface{}) {
return http.StatusMethodNotAllowed, nil
}
func (r *MethodNotAllowedResource) Get(req *http.Request, id string) (int, interface{}) {
return http.StatusMethodNotAllowed, nil
}
func (r *MethodNotAllowedResource) Create(req *http.Request, value interface{}) (int, interface{}) {
return http.StatusMethodNotAllowed, nil
}
func (r *MethodNotAllowedResource) Update(req *http.Request, id string, value interface{}) (int, interface{}) {
return http.StatusMethodNotAllowed, nil
}
func (r *MethodNotAllowedResource) Delete(req *http.Request, id string) (int, interface{}) {
return http.StatusMethodNotAllowed, nil
}
func (r *MethodNotAllowedResource) Action(req *http.Request, id, action string) (int, interface{}) {
return http.StatusMethodNotAllowed, nil
}