Update vendoring of github.com/prometheus/common/route

This commit is contained in:
Julius Volz 2016-09-21 02:51:34 +02:00
parent 4520e12440
commit 766074568e
2 changed files with 42 additions and 30 deletions

View File

@ -32,10 +32,38 @@ func WithParam(ctx context.Context, p, v string) context.Context {
return context.WithValue(ctx, param(p), v)
}
// handle turns a Handle into httprouter.Handle
func handle(h http.HandlerFunc) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
ctx, cancel := context.WithCancel(context.Background())
type contextFn func(r *http.Request) context.Context
// Router wraps httprouter.Router and adds support for prefixed sub-routers
// and per-request context injections.
type Router struct {
rtr *httprouter.Router
prefix string
ctxFn contextFn
}
// New returns a new Router.
func New(ctxFn contextFn) *Router {
if ctxFn == nil {
ctxFn = func(r *http.Request) context.Context {
return context.Background()
}
}
return &Router{
rtr: httprouter.New(),
ctxFn: ctxFn,
}
}
// WithPrefix returns a router that prefixes all registered routes with prefix.
func (r *Router) WithPrefix(prefix string) *Router {
return &Router{rtr: r.rtr, prefix: r.prefix + prefix, ctxFn: r.ctxFn}
}
// handle turns a HandlerFunc into an httprouter.Handle.
func (r *Router) handle(h http.HandlerFunc) httprouter.Handle {
return func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
ctx, cancel := context.WithCancel(r.ctxFn(req))
defer cancel()
for _, p := range params {
@ -43,56 +71,40 @@ func handle(h http.HandlerFunc) httprouter.Handle {
}
mtx.Lock()
ctxts[r] = ctx
ctxts[req] = ctx
mtx.Unlock()
h(w, r)
h(w, req)
mtx.Lock()
delete(ctxts, r)
delete(ctxts, req)
mtx.Unlock()
}
}
// Router wraps httprouter.Router and adds support for prefixed sub-routers.
type Router struct {
rtr *httprouter.Router
prefix string
}
// New returns a new Router.
func New() *Router {
return &Router{rtr: httprouter.New()}
}
// WithPrefix returns a router that prefixes all registered routes with prefix.
func (r *Router) WithPrefix(prefix string) *Router {
return &Router{rtr: r.rtr, prefix: r.prefix + prefix}
}
// Get registers a new GET route.
func (r *Router) Get(path string, h http.HandlerFunc) {
r.rtr.GET(r.prefix+path, handle(h))
r.rtr.GET(r.prefix+path, r.handle(h))
}
// Options registers a new OPTIONS route.
func (r *Router) Options(path string, h http.HandlerFunc) {
r.rtr.OPTIONS(r.prefix+path, handle(h))
r.rtr.OPTIONS(r.prefix+path, r.handle(h))
}
// Del registers a new DELETE route.
func (r *Router) Del(path string, h http.HandlerFunc) {
r.rtr.DELETE(r.prefix+path, handle(h))
r.rtr.DELETE(r.prefix+path, r.handle(h))
}
// Put registers a new PUT route.
func (r *Router) Put(path string, h http.HandlerFunc) {
r.rtr.PUT(r.prefix+path, handle(h))
r.rtr.PUT(r.prefix+path, r.handle(h))
}
// Post registers a new POST route.
func (r *Router) Post(path string, h http.HandlerFunc) {
r.rtr.POST(r.prefix+path, handle(h))
r.rtr.POST(r.prefix+path, r.handle(h))
}
// Redirect takes an absolute path and sends an internal HTTP redirect for it,

4
vendor/vendor.json vendored
View File

@ -211,8 +211,8 @@
{
"checksumSHA1": "CKVJRc1NREmfoAWQLHxqWQlvxo0=",
"path": "github.com/prometheus/common/route",
"revision": "4402f4e5ea79ec15f3c574773b6a5198fbea215f",
"revisionTime": "2016-06-23T15:14:27Z"
"revision": "ee21c31a8cbad2a29cce94a8c53855e1dab92bf7",
"revisionTime": "2016-09-21T02:27:24+02:00"
},
{
"checksumSHA1": "91KYK0SpvkaMJJA2+BcxbVnyRO0=",