-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathmiddleware_19.go
104 lines (99 loc) · 3.27 KB
/
middleware_19.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
97
98
99
100
101
102
103
104
// +build go1.9
package kami
import (
"context"
"fmt"
"net/http"
"github.com/zenazn/goji/web/mutil"
)
// convert turns standard http middleware into kami Middleware if needed.
func convert(mw MiddlewareType) Middleware {
switch x := mw.(type) {
case Middleware:
return x
case func(context.Context, http.ResponseWriter, *http.Request) context.Context:
return Middleware(x)
case func(ContextHandler) ContextHandler:
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
var dh dummyHandler
x(&dh).ServeHTTPContext(ctx, w, r)
if !dh {
return nil
}
return ctx
}
case func(http.Handler) http.Handler:
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
var dh dummyHandler
x(&dh).ServeHTTP(w, r)
if !dh {
return nil
}
return ctx
}
case http.Handler:
return Middleware(func(_ context.Context, w http.ResponseWriter, r *http.Request) context.Context {
x.ServeHTTP(w, r)
return r.Context()
})
case func(w http.ResponseWriter, r *http.Request):
return Middleware(func(_ context.Context, w http.ResponseWriter, r *http.Request) context.Context {
x(w, r)
return r.Context()
})
case func(w http.ResponseWriter, r *http.Request) context.Context:
return Middleware(func(_ context.Context, w http.ResponseWriter, r *http.Request) context.Context {
return x(w, r)
})
}
panic(fmt.Errorf("unsupported MiddlewareType: %T", mw))
}
// convertAW
func convertAW(aw AfterwareType) Afterware {
switch x := aw.(type) {
case Afterware:
return x
case func(context.Context, mutil.WriterProxy, *http.Request) context.Context:
return Afterware(x)
case func(context.Context, *http.Request) context.Context:
return func(ctx context.Context, _ mutil.WriterProxy, r *http.Request) context.Context {
return x(ctx, r)
}
case func(context.Context) context.Context:
return func(ctx context.Context, _ mutil.WriterProxy, _ *http.Request) context.Context {
return x(ctx)
}
case Middleware:
return func(ctx context.Context, w mutil.WriterProxy, r *http.Request) context.Context {
return x(ctx, w, r)
}
case func(context.Context, http.ResponseWriter, *http.Request) context.Context:
return func(ctx context.Context, w mutil.WriterProxy, r *http.Request) context.Context {
return x(ctx, w, r)
}
case func(w http.ResponseWriter, r *http.Request) context.Context:
return Afterware(func(_ context.Context, w mutil.WriterProxy, r *http.Request) context.Context {
return x(w, r)
})
case func(w mutil.WriterProxy, r *http.Request) context.Context:
return Afterware(func(_ context.Context, w mutil.WriterProxy, r *http.Request) context.Context {
return x(w, r)
})
case http.Handler:
return Afterware(func(_ context.Context, w mutil.WriterProxy, r *http.Request) context.Context {
x.ServeHTTP(w, r)
return r.Context()
})
case func(w http.ResponseWriter, r *http.Request):
return Afterware(func(_ context.Context, w mutil.WriterProxy, r *http.Request) context.Context {
x(w, r)
return r.Context()
})
case func(w mutil.WriterProxy, r *http.Request):
return Afterware(func(_ context.Context, w mutil.WriterProxy, r *http.Request) context.Context {
x(w, r)
return r.Context()
})
}
panic(fmt.Errorf("unsupported AfterwareType: %T", aw))
}