/
wctx.go
125 lines (98 loc) · 3.02 KB
/
wctx.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package wctx
import (
"github.com/julienschmidt/httprouter"
paramsHelper "github.com/orian/params"
"golang.org/x/net/context"
"log"
"net/http"
)
type HandleFunc func(context.Context, http.ResponseWriter, *http.Request)
func (h HandleFunc) ServeHTTP(c context.Context, w http.ResponseWriter, r *http.Request) {
h(c, w, r)
}
type Handler interface {
ServeHTTP(context.Context, http.ResponseWriter, *http.Request)
}
type ContextProvider func(*http.Request) context.Context
type Router struct {
R *httprouter.Router
ContextFactory ContextProvider
m []Middleware
}
var paramsKey = "params key"
func (r *Router) wrap(h HandleFunc) httprouter.Handle {
// builds wrapper
for i := len(r.m) - 1; i >= 0; i-- {
h = r.m[i](h)
}
return httprouter.Handle(func(w http.ResponseWriter, req *http.Request, p httprouter.Params) {
ctx := r.ContextFactory(req)
if p != nil {
ctx = context.WithValue(ctx, ¶msKey, p)
}
h(ctx, w, req)
})
}
func FromContext(c context.Context) (paramsHelper.Params, bool) {
p, ok := c.Value(¶msKey).(httprouter.Params)
return paramsHelper.NewFromHttpRouter(p), ok
}
func DefaultContextFactory(req *http.Request) context.Context {
return context.TODO()
}
func New() *Router {
return &Router{httprouter.New(), DefaultContextFactory, nil}
}
func (r *Router) DELETE(path string, handle HandleFunc) {
r.R.DELETE(path, r.wrap(handle))
}
func (r *Router) GET(path string, handle HandleFunc) {
r.R.GET(path, r.wrap(handle))
}
func (r *Router) HEAD(path string, handle HandleFunc) {
r.R.HEAD(path, r.wrap(handle))
}
func (r *Router) Handle(method, path string, handle HandleFunc) {
r.R.Handle(method, path, r.wrap(handle))
}
func (r *Router) Handler(method, path string, handler http.Handler) {
if len(r.m) > 0 {
log.Printf("warning: when registering standard http.Handler the middlewares are skipped")
}
r.R.Handler(method, path, handler)
}
func (r *Router) HandlerFunc(method, path string, handler http.HandlerFunc) {
if len(r.m) > 0 {
log.Printf("warning: when registering standard http.HandleFunc the middlewares are skipped")
}
r.R.HandlerFunc(method, path, handler)
}
func (r *Router) Lookup(method, path string) (httprouter.Handle, httprouter.Params, bool) {
return r.R.Lookup(method, path)
}
func (r *Router) OPTIONS(path string, handle HandleFunc) {
r.R.OPTIONS(path, r.wrap(handle))
}
func (r *Router) PATCH(path string, handle HandleFunc) {
r.R.PATCH(path, r.wrap(handle))
}
func (r *Router) POST(path string, handle HandleFunc) {
r.R.POST(path, r.wrap(handle))
}
func (r *Router) PUT(path string, handle HandleFunc) {
r.R.PUT(path, r.wrap(handle))
}
func (r *Router) ServeFiles(path string, root http.FileSystem) {
r.R.ServeFiles(path, root)
}
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
r.R.ServeHTTP(w, req)
}
type Middleware func(HandleFunc) HandleFunc
func (r *Router) WithMiddleware(m Middleware) *Router {
newR := *r
newR.m = make([]Middleware, 0, len(r.m)+1)
newR.m = append(newR.m, r.m...)
newR.m = append(newR.m, m)
return &newR
}