/
apimethod.go
465 lines (381 loc) · 15.3 KB
/
apimethod.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
package goapi;
import (
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"reflect"
"runtime"
"strconv"
"strings"
"time"
)
const (
MULTIPART_PARSE_SIZE = 4 * 1024 * 1024 // Store the first 4M in memory.
PARAM_TOKEN = "token"
)
const (
API_PARAM_TYPE_INT = iota
API_PARAM_TYPE_STRING
API_PARAM_TYPE_FILE
)
// We need to define these as types, so we can figure it out when we want to pass params.
type Token string;
type UserId int;
type UserName string;
type File struct {
Io *multipart.File
}
func (file File) Valid() bool {
return file.Io != nil;
}
func (file File) Data() ([]byte, error) {
if (!file.Valid()) {
return nil, fmt.Errorf("Cannot get data of invalid file");
}
return ioutil.ReadAll(*file.Io);
}
type ApiMethod struct {
path string
handler interface{}
auth bool
allowTokenParam bool
params []ApiMethodParam
log Logger
serializer Serializer
contentType string
errorResponder ErrorResponder
tokenValidator ValidateToken
}
type ApiMethodParam struct {
Name string
ParamType int
Required bool
}
func (method ApiMethod) Path() string {
return method.path;
}
// Returns this so you can chain.
func (method *ApiMethod) SetAllowTokenParam(val bool) *ApiMethod {
method.allowTokenParam = val;
return method;
}
// Will just panic on error.
func (method ApiMethod) validate() {
// Check the definitions.
if (method.path == "") {
method.log.Panic("Bad path for API handler");
}
if (method.handler == nil) {
method.log.Panic(fmt.Sprintf("Nil handler for API handler for path: %s", method.path));
}
for _, param := range(method.params) {
if (param.Name == "") {
method.log.Panic(fmt.Sprintf("Empty name for param for API handler for path: %s", method.path));
}
if (!(param.ParamType == API_PARAM_TYPE_INT || param.ParamType == API_PARAM_TYPE_STRING || param.ParamType == API_PARAM_TYPE_FILE)) {
method.log.Panic(fmt.Sprintf("Param (%s) for API handler (%s) has bad type (%d)", param.Name, method.path, param.ParamType));
}
}
// Check parameter semantics.
var handlerType reflect.Type = reflect.TypeOf(method.handler);
var numParams int = handlerType.NumIn();
var additionalParams = 0;
for i := 0; i < numParams; i++ {
var ParamType reflect.Type = handlerType.In(i);
if (ParamType.String() == "goapi.Token") {
additionalParams++;
if (!method.auth) {
method.log.Panic(fmt.Sprintf("API handler (%s) requested a token without authentication", method.path));
}
} else if (ParamType.String() == "goapi.UserId") {
additionalParams++;
if (!method.auth) {
method.log.Panic(fmt.Sprintf("API handler (%s) requested a user id without authentication", method.path));
}
} else if (ParamType.String() == "goapi.UserName") {
additionalParams++;
if (!method.auth) {
method.log.Panic(fmt.Sprintf("API handler (%s) requested a user name without authentication", method.path));
}
} else if (ParamType.String() == "*http.Request") {
additionalParams++;
} else if (ParamType.String() == "http.ResponseWriter") {
additionalParams++;
} else {
if (!(ParamType.String() == "int" || ParamType.String() == "string" || ParamType.String() == "goapi.File")) {
method.log.Panic(fmt.Sprintf("API handler (%s) has an actual parameter with incorrect type (%s) must be string or int", method.path, ParamType.String()));
}
}
}
if (numParams != len(method.params) + additionalParams) {
method.log.Panic(fmt.Sprintf("API handler (%s) actually expects %d parameters, but is defined to expect %d (%d defined, %d implicit)", method.path, numParams, len(method.params) + additionalParams, len(method.params), additionalParams));
}
// Check the return semantics.
var numReturns int = handlerType.NumOut();
if (numReturns > 4) {
method.log.Panic(fmt.Sprintf("API handler (%s) has too many return values. Got %d. Maximum is 4.", method.path, numReturns));
}
// All of the return types are unique.
var seenReturnTypes map[string]bool = make(map[string]bool);
for i := 0; i < numReturns; i++ {
var returnType string = handlerType.Out(i).String();
if (!(returnType == "interface {}" || returnType == "int" || returnType == "string" || returnType == "error")) {
method.log.Panic(fmt.Sprintf("API handler (%s) has an bad return type (%s) must be interface{}, int, string, or error", method.path, returnType));
}
if (seenReturnTypes[returnType]) {
method.log.Panic(fmt.Sprintf("API handler (%s) has duplicate return types (%s). Can only have up to four return types and each must be a unique type (interface{}, int, string, or error).", method.path, returnType));
}
seenReturnTypes[returnType] = true;
}
}
func (method ApiMethod) Middleware() func(response http.ResponseWriter, request *http.Request) {
return func(response http.ResponseWriter, request *http.Request) {
// Skip preflight checks.
if (request.Method == "OPTIONS") {
return;
}
if (request.URL != nil) {
method.log.Debug(request.URL.String());
}
responseObj, httpStatus, contentType, err := method.handleAPIRequest(response, request);
response.Header().Set("Content-Type", contentType);
if (err != nil) {
method.sendResponse("", err, httpStatus, response);
return;
}
// Check to see if we need to close the caller's response object when we are done.
defer func() {
closer, ok := responseObj.(io.Closer);
if (ok) {
err = closer.Close();
if (err != nil) {
method.log.WarnE("Error closing a response reader, but the response still went out fine.", err);
}
}
}();
// If the response object is an io.ReadSeeker, then we will let
// http.ServeContent take care of almost all the work
// (except setting the content type).
readSeeker, ok := responseObj.(io.ReadSeeker);
if (ok) {
http.ServeContent(response, request, "", time.Time{}, readSeeker);
return;
}
// Set some standard headers.
response.Header().Set("Access-Control-Allow-Origin", "*");
response.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
response.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, Authorization");
// If the response object is a reader, then stream it into the response writer.
reader, ok := responseObj.(io.Reader);
if (ok) {
if (httpStatus == 0) {
httpStatus = http.StatusOK;
}
response.WriteHeader(httpStatus);
_, err = io.Copy(response, reader);
if (err != nil) {
// The reeponse may have got partially written... so just abandon the request.
method.log.ErrorE("Failed to stream the response", err);
}
} else {
// Otherwise, just serialize the response and send it over.
responseString, err := method.serializer(responseObj);
method.sendResponse(responseString, err, httpStatus, response);
}
}
}
// This handles the API side of the request.
// None of the boilerplate.
func (method ApiMethod) handleAPIRequest(response http.ResponseWriter, request *http.Request) (interface{}, int, string, error) {
var userId int = -1;
var userName string = "";
var ok bool;
var token string = "";
if (method.auth) {
var responseObject interface{};
ok, userId, userName, token, responseObject = method.authRequest(request);
if (!ok) {
return responseObject, http.StatusUnauthorized, method.contentType, nil;
}
}
ok, args := method.createArguments(UserId(userId), UserName(userName), Token(token), response, request);
if (!ok) {
return method.errorResponder(nil, http.StatusBadRequest), http.StatusBadRequest, method.contentType, nil;
}
var handlerValue reflect.Value = reflect.ValueOf(method.handler);
returns := handlerValue.Call(args);
return method.createReturnValues(returns);
}
func (method ApiMethod) createReturnValues(returns []reflect.Value) (interface{}, int, string, error) {
var responseObj interface{} = nil;
var httpStatus int = 0;
var err error = nil;
var contentType string = method.contentType;
// Returns are optional.
for _, val := range(returns) {
var returnType reflect.Type = val.Type();
if (returnType.String() == "interface {}") {
if (!val.IsNil()) {
responseObj = val.Interface();
}
} else if (returnType.String() == "int") {
httpStatus = int(val.Int());
} else if (returnType.String() == "string") {
if (val.String() != "") {
contentType = val.String();
}
} else if (returnType.String() == "error") {
if (!val.IsNil()) {
err = val.Interface().(error);
}
} else {
method.log.Fatal(fmt.Sprintf("Unkown return type (%s) for API handler for path: %s", returnType.String(), method.path));
}
}
return responseObj, httpStatus, contentType, err;
}
// Get all the parameters setup for invocation.
func (method ApiMethod) createArguments(userId UserId, userName UserName, token Token, response http.ResponseWriter, request *http.Request) (bool, []reflect.Value) {
var handlerType reflect.Type = reflect.TypeOf(method.handler);
var numParams int = handlerType.NumIn();
var apiParamIndex = 0;
var paramValues []reflect.Value = make([]reflect.Value, numParams);
for i := 0; i < numParams; i++ {
var ParamType reflect.Type = handlerType.In(i);
// The user id, token, request, and response get handled specially.
if (method.auth && ParamType.String() == "goapi.Token") {
paramValues[i] = reflect.ValueOf(token);
} else if (method.auth && ParamType.String() == "goapi.UserId") {
paramValues[i] = reflect.ValueOf(userId);
} else if (method.auth && ParamType.String() == "goapi.UserName") {
paramValues[i] = reflect.ValueOf(userName);
} else if (ParamType.String() == "*http.Request") {
paramValues[i] = reflect.ValueOf(request);
} else if (ParamType.String() == "http.ResponseWriter") {
paramValues[i] = reflect.ValueOf(response);
} else {
// Normal param, fetch the next api parameter and pass it along.
ok, val := method.fetchParam(apiParamIndex, request);
if (!ok) {
return false, []reflect.Value{};
}
paramValues[i] = val;
apiParamIndex++;
}
}
return true, paramValues;
}
func (method ApiMethod) fetchParam(apiParamIndex int, request *http.Request) (bool, reflect.Value) {
// Only the first call will do anything.
request.ParseMultipartForm(MULTIPART_PARSE_SIZE);
var param ApiMethodParam = method.params[apiParamIndex];
if (param.ParamType == API_PARAM_TYPE_FILE) {
file, _, err := request.FormFile(param.Name);
if (err != nil) {
if (param.Required) {
method.log.Warn(fmt.Sprintf("Required file parameter not found: %s", param.Name));
return false, reflect.Value{};
} else {
return true, reflect.ValueOf(File{nil});
}
}
return true, reflect.ValueOf(File{&file});
}
var stringValue string = strings.TrimSpace(request.FormValue(param.Name));
if (param.Required && stringValue == "") {
method.log.Warn(fmt.Sprintf("Required parameter not found: %s", param.Name));
return false, reflect.Value{};
}
// If we are looking for string, then we are done.
if (param.ParamType == API_PARAM_TYPE_STRING) {
return true, reflect.ValueOf(stringValue);
}
// We must be looking for an int (only ints, files, and strings are allowed).
// First check for an empty non-required int.
if (stringValue == "") {
return true, reflect.ValueOf(0);
}
intValue, err := strconv.Atoi(stringValue);
if (err != nil) {
method.log.WarnE(fmt.Sprintf("Unable to convert int parameter (%s) from string: '%s'", param.Name, stringValue), err);
return false, reflect.ValueOf(0);
}
return true, reflect.ValueOf(intValue);
}
// Send a response over |response|.
// On error, |responseString| will be ignored.
// In not supplied, the |httpStatus| will become http.StatusInternalServerError on error and
// http.StatusOK on success.
func (method ApiMethod) sendResponse(responseString string, err error, httpStatus int, response http.ResponseWriter) {
if (err != nil) {
method.log.ErrorE("API Error", err);
if (httpStatus == 0) {
httpStatus = http.StatusInternalServerError;
}
// Any serialization errors will be ignored at this point.
responseString, _ = method.serializer(method.errorResponder(err, httpStatus));
response.WriteHeader(httpStatus);
fmt.Fprintln(response, responseString);
} else {
method.log.Debug("Successful Response:\n" + responseString);
if (httpStatus == 0) {
httpStatus = http.StatusOK;
}
response.WriteHeader(httpStatus)
fmt.Fprintln(response, responseString);
}
}
// Tries to authorize a request.
// Returns: success, user id, user name, request token, and response object.
// user id and token will only be populated on success.
// response object will only be populated on error.
func (method ApiMethod) authRequest(request *http.Request) (bool, int, string, string, interface{}) {
token, ok := getToken(request, method.allowTokenParam);
if (!ok) {
return false, 0, "", "", method.errorResponder(TokenValidationError{TOKEN_VALIDATION_NO_TOKEN}, http.StatusUnauthorized);
}
// Check for empty tokens.
if (strings.TrimSpace(token) == "") {
return false, 0, "", "", method.errorResponder(TokenValidationError{TOKEN_VALIDATION_NO_TOKEN}, http.StatusUnauthorized);
}
userId, userName, err := method.tokenValidator(token, method.log);
if (err != nil) {
validationErr, ok := err.(TokenValidationError);
if (!ok) {
// Some other (non-validation) error.
return false, 0, "", "", method.errorResponder(nil, http.StatusInternalServerError);
}
return false, 0, "", "", method.errorResponder(validationErr, http.StatusUnauthorized);
}
return true, userId, userName, token, nil;
}
func (method ApiMethod) String() string {
var rtn string = "";
rtn += fmt.Sprintf("%s\n", method.path);
rtn += fmt.Sprintf(" Authentication Required: %v\n", method.auth);
if (len(method.params) == 0) {
rtn += " Params: None\n"
} else {
rtn += " Params:\n"
for _, param := range(method.params) {
rtn += fmt.Sprintf(" %v\n", param);
}
}
rtn += fmt.Sprintf(" Handler: %s ", runtime.FuncForPC(reflect.ValueOf(method.handler).Pointer()).Name());
return rtn;
}
func (param ApiMethodParam) String() string {
var typeString string = "int";
if (param.ParamType == API_PARAM_TYPE_STRING) {
typeString = "string";
} else if (param.ParamType == API_PARAM_TYPE_FILE) {
typeString = "File";
}
var requiredString string = "";
if (param.Required) {
requiredString = " (required)";
}
return fmt.Sprintf("%s %s%s", param.Name, typeString, requiredString);
}