-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlerconv.go
66 lines (58 loc) · 1.67 KB
/
handlerconv.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
package gin
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
// Wrap converts native http.Handler & http.HandlerFunc to gin.HandlerFunc.
//
// Supported form types:
//
// .Wrap(h http.Handler)
// .Wrap(func(w http.ResponseWriter, r *http.Request))
// .Wrap(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc))
func Wrap(handler interface{}) gin.HandlerFunc {
switch h := handler.(type) {
case gin.HandlerFunc:
return h
case http.Handler:
return func(ctx *gin.Context) {
h.ServeHTTP(ctx.Writer, ctx.Request)
}
case func(http.ResponseWriter, *http.Request):
return Wrap(http.HandlerFunc(h))
case func(http.ResponseWriter, *http.Request, http.HandlerFunc):
return FromStdWithNext(h)
default:
{
panic(fmt.Errorf(`
Passed argument is not a func(*gin.Context) neither one of these types:
- http.Handler
- func(w http.ResponseWriter, r *http.Request)
- func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)
---------------------------------------------------------------------
It seems to be a %T points to: %v`, handler, handler))
}
}
}
func FromStdWithNext(h func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)) gin.HandlerFunc {
return func(ctx *gin.Context) {
next := func(w http.ResponseWriter, r *http.Request) {
ctx.Request = r
ctx.Next()
}
h(ctx.Writer, ctx.Request, next)
}
}
func Converts(handlers []http.HandlerFunc) []gin.HandlerFunc {
var rets []gin.HandlerFunc
for _, handler := range handlers {
rets = append(rets, Wrap(handler))
}
return rets
}
func Convert(handler http.HandlerFunc) gin.HandlerFunc {
return func(ctx *gin.Context) {
handler(ctx.Writer, ctx.Request)
}
}