-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
96 lines (78 loc) · 2.77 KB
/
middleware.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
package request
import (
"github.com/swaggest/fchi"
"github.com/swaggest/rest"
"github.com/swaggest/rest-fasthttp/fhttp"
"github.com/swaggest/usecase"
"github.com/valyala/fasthttp"
)
type requestDecoderSetter interface {
SetRequestDecoder(fhttp.RequestDecoder)
}
type requestMapping interface {
RequestMapping() rest.RequestMapping
}
// DecoderMiddleware sets up request decoder in suitable handlers.
func DecoderMiddleware(factory DecoderMaker) func(fchi.Handler) fchi.Handler {
return func(handler fchi.Handler) fchi.Handler {
var (
withRoute rest.HandlerWithRoute
withUseCase rest.HandlerWithUseCase
withRequestMapping requestMapping
setRequestDecoder requestDecoderSetter
useCaseWithInput usecase.HasInputPort
)
if !fhttp.HandlerAs(handler, &setRequestDecoder) ||
!fhttp.HandlerAs(handler, &withRoute) ||
!fhttp.HandlerAs(handler, &withUseCase) ||
!usecase.As(withUseCase.UseCase(), &useCaseWithInput) {
return handler
}
var customMapping rest.RequestMapping
if fhttp.HandlerAs(handler, &withRequestMapping) {
customMapping = withRequestMapping.RequestMapping()
}
input := useCaseWithInput.InputPort()
if input != nil {
setRequestDecoder.SetRequestDecoder(
factory.MakeDecoder(withRoute.RouteMethod(), useCaseWithInput.InputPort(), customMapping),
)
}
return handler
}
}
type withRestHandler interface {
RestHandler() *rest.HandlerTrait
}
// ValidatorMiddleware sets up request validator in suitable handlers.
func ValidatorMiddleware(factory rest.RequestValidatorFactory) func(fchi.Handler) fchi.Handler {
return func(handler fchi.Handler) fchi.Handler {
var (
withRoute rest.HandlerWithRoute
withUseCase rest.HandlerWithUseCase
handlerTrait withRestHandler
useCaseWithInput usecase.HasInputPort
)
if !fhttp.HandlerAs(handler, &handlerTrait) ||
!fhttp.HandlerAs(handler, &withRoute) ||
!fhttp.HandlerAs(handler, &withUseCase) ||
!usecase.As(withUseCase.UseCase(), &useCaseWithInput) {
return handler
}
rh := handlerTrait.RestHandler()
rh.ReqValidator = factory.MakeRequestValidator(
withRoute.RouteMethod(), useCaseWithInput.InputPort(), rh.ReqMapping)
return handler
}
}
var _ fhttp.RequestDecoder = DecoderFunc(nil)
// DecoderFunc implements RequestDecoder with a func.
type DecoderFunc func(rc *fasthttp.RequestCtx, input interface{}, validator rest.Validator) error
// Decode implements RequestDecoder.
func (df DecoderFunc) Decode(rc *fasthttp.RequestCtx, input interface{}, validator rest.Validator) error {
return df(rc, input, validator)
}
// DecoderMaker creates request decoder for particular structured Go input value.
type DecoderMaker interface {
MakeDecoder(method string, input interface{}, customMapping rest.RequestMapping) fhttp.RequestDecoder
}