-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
70 lines (57 loc) · 1.73 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
package jin
import (
"fmt"
"github.com/juanjiTech/inject/v2"
"net/http"
"reflect"
)
// HandlerFunc defines the handler used by Jin middleware as return value.
type HandlerFunc interface{}
// HandlersChain defines a HandlerFunc slice.
type HandlersChain []HandlerFunc
// Last returns the last handler in the chain. i.e. the last handler is the main one.
func (c HandlersChain) Last() HandlerFunc {
if length := len(c); length > 0 {
return c[length-1]
}
return nil
}
var _ inject.FastInvoker = (*ContextInvoker)(nil)
// ContextInvoker is an inject.FastInvoker implementation of `func(Context)`.
type ContextInvoker func(ctx *Context)
func (invoke ContextInvoker) Invoke(args []interface{}) ([]reflect.Value, error) {
invoke(args[0].(*Context))
return nil, nil
}
// httpHandlerFuncInvoker is an inject.FastInvoker implementation of
// `func(http.ResponseWriter, *http.Request)`.
type httpHandlerFuncInvoker func(http.ResponseWriter, *http.Request)
func (invoke httpHandlerFuncInvoker) Invoke(args []interface{}) ([]reflect.Value, error) {
invoke(args[0].(http.ResponseWriter), args[1].(*http.Request))
return nil, nil
}
func fastInvokeWarpHandler(h HandlerFunc) HandlerFunc {
if reflect.TypeOf(h).Kind() != reflect.Func {
panic(fmt.Sprintf("handler must be a callable function, but got %T", h))
}
if inject.IsFastInvoker(h) {
return h
}
switch v := h.(type) {
case func(*Context):
return ContextInvoker(v)
case func(http.ResponseWriter, *http.Request):
return httpHandlerFuncInvoker(v)
case http.HandlerFunc:
return httpHandlerFuncInvoker(v)
}
return h
}
func fastInvokeWarpHandlerChain(hc HandlersChain) {
for i, handlerFunc := range hc {
if handlerFunc == nil {
continue
}
hc[i] = fastInvokeWarpHandler(handlerFunc)
}
}