-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathadapter.go
More file actions
275 lines (259 loc) · 6.92 KB
/
adapter.go
File metadata and controls
275 lines (259 loc) · 6.92 KB
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
package fiber
import (
"fmt"
"net/http"
"reflect"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttpadaptor"
)
// toFiberHandler converts a supported handler type to a Fiber handler.
func toFiberHandler(handler any) (Handler, bool) {
if handler == nil {
return nil, false
}
switch handler.(type) {
case Handler, func(Ctx): // (1)-(2) Fiber handlers
return adaptFiberHandler(handler)
case func(Req, Res) error, func(Req, Res), func(Req, Res, func() error) error, func(Req, Res, func() error), func(Req, Res, func()) error, func(Req, Res, func()), func(Req, Res, func(error)), func(Req, Res, func(error)) error, func(Req, Res, func(error) error), func(Req, Res, func(error) error) error: // (3)-(12) Express-style request handlers
return adaptExpressHandler(handler)
case http.HandlerFunc, http.Handler, func(http.ResponseWriter, *http.Request): // (13)-(15) net/http handlers
return adaptHTTPHandler(handler)
case fasthttp.RequestHandler, func(*fasthttp.RequestCtx) error: // (16)-(17) fasthttp handlers
return adaptFastHTTPHandler(handler)
default: // (18) unsupported handler type
return nil, false
}
}
func adaptFiberHandler(handler any) (Handler, bool) {
switch h := handler.(type) {
case Handler: // (1) direct Fiber handler
if h == nil {
return nil, false
}
return h, true
case func(Ctx): // (2) Fiber handler without error return
if h == nil {
return nil, false
}
return func(c Ctx) error {
h(c)
return nil
}, true
default:
return nil, false
}
}
func adaptExpressHandler(handler any) (Handler, bool) {
switch h := handler.(type) {
case func(Req, Res) error: // (3) Express-style handler with error return
if h == nil {
return nil, false
}
return func(c Ctx) error {
return h(c.Req(), c.Res())
}, true
case func(Req, Res): // (4) Express-style handler without error return
if h == nil {
return nil, false
}
return func(c Ctx) error {
h(c.Req(), c.Res())
return nil
}, true
case func(Req, Res, func() error) error: // (5) Express-style handler with error-returning next callback and error return
if h == nil {
return nil, false
}
return func(c Ctx) error {
return h(c.Req(), c.Res(), func() error {
return c.Next()
})
}, true
case func(Req, Res, func() error): // (6) Express-style handler with error-returning next callback
if h == nil {
return nil, false
}
return func(c Ctx) error {
var nextErr error
h(c.Req(), c.Res(), func() error {
nextErr = c.Next()
return nextErr
})
return nextErr
}, true
case func(Req, Res, func()) error: // (7) Express-style handler with no-arg next callback and error return
if h == nil {
return nil, false
}
return func(c Ctx) error {
var nextErr error
err := h(c.Req(), c.Res(), func() {
nextErr = c.Next()
})
if err != nil {
return err
}
return nextErr
}, true
case func(Req, Res, func()): // (8) Express-style handler with no-arg next callback
if h == nil {
return nil, false
}
return func(c Ctx) error {
var nextErr error
h(c.Req(), c.Res(), func() {
nextErr = c.Next()
})
return nextErr
}, true
case func(Req, Res, func(error)): // (9) Express-style handler with error-accepting next callback
if h == nil {
return nil, false
}
return func(c Ctx) error {
var nextErr error
h(c.Req(), c.Res(), func(err error) {
if err != nil {
nextErr = err
return
}
nextErr = c.Next()
})
return nextErr
}, true
case func(Req, Res, func(error)) error: // (10) Express-style handler with error-accepting next callback and error return
if h == nil {
return nil, false
}
return func(c Ctx) error {
var nextErr error
err := h(c.Req(), c.Res(), func(nextErrArg error) {
if nextErrArg != nil {
nextErr = nextErrArg
return
}
nextErr = c.Next()
})
if err != nil {
return err
}
return nextErr
}, true
case func(Req, Res, func(error) error): // (11) Express-style handler with error-accepting next callback that returns an error
if h == nil {
return nil, false
}
return func(c Ctx) error {
var nextErr error
h(c.Req(), c.Res(), func(nextErrArg error) error {
if nextErrArg != nil {
nextErr = nextErrArg
return nextErrArg
}
nextErr = c.Next()
return nextErr
})
return nextErr
}, true
case func(Req, Res, func(error) error) error: // (12) Express-style handler with error-accepting next callback that returns an error and error return
if h == nil {
return nil, false
}
return func(c Ctx) error {
var nextErr error
err := h(c.Req(), c.Res(), func(nextErrArg error) error {
if nextErrArg != nil {
nextErr = nextErrArg
return nextErrArg
}
nextErr = c.Next()
return nextErr
})
if err != nil {
return err
}
return nextErr
}, true
default:
return nil, false
}
}
func adaptHTTPHandler(handler any) (Handler, bool) {
switch h := handler.(type) {
case http.HandlerFunc: // (13) net/http HandlerFunc
if h == nil {
return nil, false
}
return wrapHTTPHandler(h), true
case http.Handler: // (14) net/http Handler implementation
if h == nil {
return nil, false
}
hv := reflect.ValueOf(h)
if isNilableKind(hv.Kind()) && hv.IsNil() {
return nil, false
}
return wrapHTTPHandler(h), true
case func(http.ResponseWriter, *http.Request): // (15) net/http function handler
if h == nil {
return nil, false
}
return wrapHTTPHandler(http.HandlerFunc(h)), true
default:
return nil, false
}
}
func isNilableKind(kind reflect.Kind) bool {
switch kind {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Pointer, reflect.Interface, reflect.Slice, reflect.UnsafePointer:
return true
default:
return false
}
}
func adaptFastHTTPHandler(handler any) (Handler, bool) {
switch h := handler.(type) {
case fasthttp.RequestHandler: // (16) fasthttp handler
if h == nil {
return nil, false
}
return func(c Ctx) error {
h(c.RequestCtx())
return nil
}, true
case func(*fasthttp.RequestCtx) error: // (17) fasthttp handler with error return
if h == nil {
return nil, false
}
return func(c Ctx) error {
return h(c.RequestCtx())
}, true
default:
return nil, false
}
}
// wrapHTTPHandler adapts a net/http handler to a Fiber handler.
func wrapHTTPHandler(handler http.Handler) Handler {
if handler == nil {
return nil
}
adapted := fasthttpadaptor.NewFastHTTPHandler(handler)
return func(c Ctx) error {
adapted(c.RequestCtx())
return nil
}
}
// collectHandlers converts a slice of handler arguments to Fiber handlers.
// The context string is used to provide informative panic messages when an
// unsupported handler type is encountered.
func collectHandlers(context string, args ...any) []Handler {
handlers := make([]Handler, 0, len(args))
for i, arg := range args {
handler, ok := toFiberHandler(arg)
if !ok {
panic(fmt.Sprintf("%s: invalid handler #%d (%T)\n", context, i, arg))
}
handlers = append(handlers, handler)
}
return handlers
}