-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
364 lines (316 loc) · 9.04 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
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
/*
Convert Go func to http.HandleFunc that handle json request and response json
*/
package jsonhandlerfunc
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"reflect"
)
type Config struct {
ErrHandler func(oldErr error) (newErr error)
}
var defaultConfig *Config = &Config{}
func (cfg *Config) injectedParams(w http.ResponseWriter, r *http.Request, injectFunc interface{}, ft reflect.Type) (injVals []reflect.Value, shouldReturn bool) {
if injectFunc == nil {
return
}
v := reflect.ValueOf(injectFunc)
outVals := v.Call([]reflect.Value{reflect.ValueOf(w), reflect.ValueOf(r)})
var httpCode int
var err error
httpCode, _, injVals, err = cfg.returnVals(outVals)
if err != nil {
cfg.returnError(ft, w, err, httpCode)
shouldReturn = true
}
return
}
func contextInjector(w http.ResponseWriter, r *http.Request) (ctx context.Context, err error) {
ctx = r.Context()
return
}
type needIndirectValue struct {
needIndirect bool
val interface{}
}
var errorNil = reflect.Zero(reflect.TypeOf((*error)(nil)).Elem())
/*
ToHandlerFunc convert any go func to a http.HandleFunc,
that will accept json.Unmarshal request body as parameters,
and response with a body with a return values into json.
The second argument is an arguments injector, it's parameter should be (w http.ResponseWriter, r *http.Request), and return values
Will be injected to first func's first few arguments.
*/
func ToHandlerFunc(funcs ...interface{}) http.HandlerFunc {
return defaultConfig.ToHandlerFunc(funcs...)
}
func (cfg *Config) ToHandlerFunc(funcs ...interface{}) http.HandlerFunc {
if len(funcs) == 0 {
panic("pass in one or more func, from the second one is all arguments injector.")
}
var serverFunc = funcs[0]
v := reflect.ValueOf(serverFunc)
ft := v.Type()
check(ft)
var firstIsAlsoInjector bool
var argsInjectors []interface{}
for i, injector := range funcs {
injt := reflect.TypeOf(injector)
if i == 0 {
if isInjector(injt) {
firstIsAlsoInjector = true
} else {
continue
}
}
check(injt)
if !isInjector(injt) {
panic("injector params must be func(w http.ResponseWriter, r *http.Request) ...")
}
argsInjectors = append(argsInjectors, injector)
}
// if first argument is context, use contextInjector
contextType := reflect.TypeOf((*context.Context)(nil)).Elem()
if len(funcs) == 1 && ft.NumIn() > 0 && ft.In(0).Implements(contextType) {
argsInjectors = append(argsInjectors, contextInjector)
}
if !firstIsAlsoInjector {
checkInjectorsType(ft, argsInjectors)
}
return func(w http.ResponseWriter, r *http.Request) {
var injectVals []reflect.Value
for _, injector := range argsInjectors {
thisInjectVals, shouldReturn := cfg.injectedParams(w, r, injector, ft)
if shouldReturn {
return
}
injectVals = append(injectVals, thisInjectVals...)
}
if firstIsAlsoInjector {
injectVals = append(injectVals, errorNil)
httpCode, outs, _, _ := cfg.returnVals(injectVals)
w.WriteHeader(httpCode)
writeJSONResponse(w, outs)
return
}
// log.Printf("injectVals: %#+v\n", len(injectVals))
injectedCount := len(injectVals)
var params []interface{}
var notNilParams []interface{}
numIn := ft.NumIn()
var ptrs = make([]bool, numIn)
for i := 0; i < numIn; i++ {
if i < injectedCount {
continue
}
paramType := ft.In(i)
// log.Printf("paramType: %#+v\n", paramType.String())
ptrs[i] = true
var pv interface{}
switch paramType.Kind() {
case reflect.Chan:
panic("params can not be chan type.")
case reflect.Ptr:
pv = reflect.New(paramType.Elem()).Interface()
case reflect.Array, reflect.Slice, reflect.Map:
pv = reflect.New(paramType).Interface()
ptrs[i] = false
default:
pv = reflect.New(paramType).Interface()
ptrs[i] = false
}
// log.Printf("pv: %#+v\n", pv)
params = append(params, pv)
notNilParams = append(notNilParams, pv)
}
if len(params) > 0 {
dec := json.NewDecoder(r.Body)
defer r.Body.Close()
req := Req{
Params: ¶ms,
}
err := dec.Decode(&req)
if err != nil {
log.Println("jsonhandlerfunc: decode request params error:", err)
cfg.returnError(ft, w, fmt.Errorf("decode request params error"), http.StatusUnprocessableEntity)
return
}
}
inVals := injectVals
for i, p := range params {
var val = reflect.ValueOf(p)
if !val.IsValid() {
val = reflect.ValueOf(notNilParams[i])
}
if !ptrs[i+injectedCount] {
val = reflect.Indirect(val)
}
inVals = append(inVals, val)
}
if len(inVals) != numIn {
cfg.returnError(ft, w, fmt.Errorf("require %d params, but passed in %d params", numIn, len(inVals)), http.StatusUnprocessableEntity)
return
}
outVals := v.Call(inVals)
httpCode, outs, _, _ := cfg.returnVals(outVals)
w.WriteHeader(httpCode)
writeJSONResponse(w, outs)
return
}
}
func (cfg *Config) returnVals(outVals []reflect.Value) (httpCode int, outs []interface{}, normalVals []reflect.Value, err error) {
normalVals = outVals[0 : len(outVals)-1]
httpCode = http.StatusOK
for _, nVal := range normalVals {
ov := nVal.Interface()
outs = append(outs, ov)
}
last := outVals[len(outVals)-1].Interface()
if last != nil {
err = last.(error)
if httpE, ok := last.(StatusCodeError); ok {
httpCode = httpE.StatusCode()
}
if codeWithErr, ok := last.(*errorWithStatusCode); ok {
err = codeWithErr.innerErr
}
if cfg.ErrHandler != nil {
err = cfg.ErrHandler(err)
}
outs = append(outs, &ResponseError{Error: err.Error(), Value: err})
} else {
outs = append(outs, nil)
}
return
}
func writeJSONResponse(w http.ResponseWriter, out interface{}) {
w.Header().Set("Content-Type", "application/json")
enc := json.NewEncoder(w)
err := enc.Encode(Resp{Results: out})
if err != nil {
log.Printf("writeJSONResponse Write err: %#+v\n", err)
}
}
type errorWithStatusCode struct {
HTTPStatusCode int
innerErr error
}
func (e *errorWithStatusCode) Error() string {
return fmt.Sprintf("%d: %s", e.HTTPStatusCode, e.innerErr)
}
func (e *errorWithStatusCode) StatusCode() int {
return e.HTTPStatusCode
}
// NewStatusCodeError for returning an error with http code
func NewStatusCodeError(code int, innerError error) (err error) {
err = &errorWithStatusCode{code, innerError}
return
}
// StatusCodeError for the error you returned contains a `StatusCode` method, It will be set to to http response.
type StatusCodeError interface {
StatusCode() int
}
/*
ResponseError is error of the Go func return values will be wrapped with this struct, So that error details can be exposed as json.
*/
type ResponseError struct {
Error string `json:"error,omitempty"`
Value interface{} `json:"value,omitempty"`
}
type Req struct {
Params interface{} `json:"params"`
}
type Resp struct {
Results interface{} `json:"results"`
}
func checkInjectorsType(ft reflect.Type, injectors []interface{}) {
var injectedTypes []reflect.Type
for _, inj := range injectors {
injt := reflect.TypeOf(inj)
for i := 0; i < injt.NumOut()-1; i++ {
injectedTypes = append(injectedTypes, injt.Out(i))
}
}
var argTypes []reflect.Type
for i := 0; i < ft.NumIn(); i++ {
if i >= len(injectedTypes) {
break
}
argTypes = append(argTypes, ft.In(i))
}
var injectedTypesStr = fmt.Sprintf("%+v", injectedTypes)
var argTypesStr = fmt.Sprintf("%+v", argTypes)
if !typesAssignableTo(injectedTypes, argTypes) {
panic(fmt.Sprintf("%+v params type is %s, but injecting %s", ft, argTypesStr, injectedTypesStr))
}
}
func typesAssignableTo(toTypes []reflect.Type, fromTypes []reflect.Type) bool {
if len(toTypes) != len(fromTypes) {
return false
}
if len(toTypes) == 0 {
return true
}
for i, _ := range toTypes {
if !fromTypes[i].AssignableTo(toTypes[i]) {
return false
}
}
return true
}
func check(ft reflect.Type) {
if ft.Kind() != reflect.Func {
panic("must pass in a func.")
}
if !isError(ft.Out(ft.NumOut() - 1)) {
panic("func's last return value must be error.")
}
for i := 0; i < ft.NumIn(); i++ {
if ft.In(i).Kind() == reflect.Chan {
panic("func arguments can not be chan type.")
}
}
for i := 0; i < ft.NumOut(); i++ {
if ft.Out(i).Kind() == reflect.Chan {
panic("func return values can not be chan type.")
}
}
}
func isInjector(ft reflect.Type) bool {
expectedTypes := []reflect.Type{
reflect.TypeOf((*http.ResponseWriter)(nil)).Elem(),
reflect.TypeOf((*http.Request)(nil)),
}
actualTypes := []reflect.Type{}
for i := 0; i < ft.NumIn(); i++ {
actualTypes = append(actualTypes, ft.In(i))
}
if !typesAssignableTo(actualTypes, expectedTypes) {
return false
}
return true
}
func isError(t reflect.Type) bool {
return t.Implements(reflect.TypeOf((*error)(nil)).Elem())
}
func (cfg *Config) returnError(ft reflect.Type, w http.ResponseWriter, err error, httpCode int) {
var errIndex = 0
errOuts := []interface{}{}
for i := 0; i < ft.NumOut(); i++ {
errOuts = append(errOuts, reflect.Zero(ft.Out(i)).Interface())
if isError(ft.Out(i)) {
errIndex = i
}
}
if cfg.ErrHandler != nil {
err = cfg.ErrHandler(err)
}
errOuts[errIndex] = &ResponseError{Error: err.Error(), Value: err}
w.WriteHeader(httpCode)
writeJSONResponse(w, errOuts)
return
}