-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
handler.go
85 lines (67 loc) · 2.48 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
// Copyright 2021 Flamego. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package flamego
import (
"fmt"
"net/http"
"reflect"
"github.com/flamego/flamego/inject"
)
// Handler is any callable function. Flamego attempts to inject services into
// the Handler's argument list and panics if any argument could not be fulfilled
// via dependency injection.
type Handler interface{}
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
}
var _ inject.FastInvoker = (*httpHandlerFuncInvoker)(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
}
var _ inject.FastInvoker = (*teapotInvoker)(nil)
type teapotInvoker func() (int, string)
func (invoke teapotInvoker) Invoke([]interface{}) ([]reflect.Value, error) {
ret1, ret2 := invoke()
return []reflect.Value{reflect.ValueOf(ret1), reflect.ValueOf(ret2)}, nil
}
// validateAndWrapHandler makes sure the handler is a callable function, it
// panics if not. When the handler is also convertible to any built-in
// inject.FastInvoker implementations, it wraps the handler automatically to
// gain up to 3x performance improvement.
func validateAndWrapHandler(h Handler, wrapper func(Handler) Handler) Handler {
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)
case func() (int, string):
return teapotInvoker(v)
}
if wrapper != nil {
h = wrapper(h)
}
return h
}
// validateAndWrapHandlers preforms validation and wrapping for given handlers.
func validateAndWrapHandlers(handlers []Handler, wrapper func(Handler) Handler) {
for i, h := range handlers {
handlers[i] = validateAndWrapHandler(h, wrapper)
}
}