forked from cerc-io/plugeth
289b30715d
This commit converts the dependency management from Godeps to the vendor folder, also switching the tool from godep to trash. Since the upstream tool lacks a few features proposed via a few PRs, until those PRs are merged in (if), use github.com/karalabe/trash. You can update dependencies via trash --update. All dependencies have been updated to their latest version. Parts of the build system are reworked to drop old notions of Godeps and invocation of the go vet command so that it doesn't run against the vendor folder, as that will just blow up during vetting. The conversion drops OpenCL (and hence GPU mining support) from ethash and our codebase. The short reasoning is that there's noone to maintain and having opencl libs in our deps messes up builds as go install ./... tries to build them, failing with unsatisfied link errors for the C OpenCL deps. golang.org/x/net/context is not vendored in. We expect it to be fetched by the user (i.e. using go get). To keep ci.go builds reproducible the package is "vendored" in build/_vendor.
122 lines
3.9 KiB
Go
122 lines
3.9 KiB
Go
package xhandler
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"golang.org/x/net/context"
|
|
)
|
|
|
|
// Chain is a helper for chaining middleware handlers together for easier
|
|
// management.
|
|
type Chain []func(next HandlerC) HandlerC
|
|
|
|
// Add appends a variable number of additional middleware handlers
|
|
// to the middleware chain. Middleware handlers can either be
|
|
// context-aware or non-context aware handlers with the appropriate
|
|
// function signatures.
|
|
func (c *Chain) Add(f ...interface{}) {
|
|
for _, h := range f {
|
|
switch v := h.(type) {
|
|
case func(http.Handler) http.Handler:
|
|
c.Use(v)
|
|
case func(HandlerC) HandlerC:
|
|
c.UseC(v)
|
|
default:
|
|
panic("Adding invalid handler to the middleware chain")
|
|
}
|
|
}
|
|
}
|
|
|
|
// With creates a new middleware chain from an existing chain,
|
|
// extending it with additional middleware. Middleware handlers
|
|
// can either be context-aware or non-context aware handlers
|
|
// with the appropriate function signatures.
|
|
func (c *Chain) With(f ...interface{}) *Chain {
|
|
n := make(Chain, len(*c))
|
|
copy(n, *c)
|
|
n.Add(f...)
|
|
return &n
|
|
}
|
|
|
|
// UseC appends a context-aware handler to the middleware chain.
|
|
func (c *Chain) UseC(f func(next HandlerC) HandlerC) {
|
|
*c = append(*c, f)
|
|
}
|
|
|
|
// Use appends a standard http.Handler to the middleware chain without
|
|
// losing track of the context when inserted between two context aware handlers.
|
|
//
|
|
// Caveat: the f function will be called on each request so you are better off putting
|
|
// any initialization sequence outside of this function.
|
|
func (c *Chain) Use(f func(next http.Handler) http.Handler) {
|
|
xf := func(next HandlerC) HandlerC {
|
|
return HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
|
|
n := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
next.ServeHTTPC(ctx, w, r)
|
|
})
|
|
f(n).ServeHTTP(w, r)
|
|
})
|
|
}
|
|
*c = append(*c, xf)
|
|
}
|
|
|
|
// Handler wraps the provided final handler with all the middleware appended to
|
|
// the chain and returns a new standard http.Handler instance.
|
|
// The context.Background() context is injected automatically.
|
|
func (c Chain) Handler(xh HandlerC) http.Handler {
|
|
ctx := context.Background()
|
|
return c.HandlerCtx(ctx, xh)
|
|
}
|
|
|
|
// HandlerFC is a helper to provide a function (HandlerFuncC) to Handler().
|
|
//
|
|
// HandlerFC is equivalent to:
|
|
// c.Handler(xhandler.HandlerFuncC(xhc))
|
|
func (c Chain) HandlerFC(xhf HandlerFuncC) http.Handler {
|
|
ctx := context.Background()
|
|
return c.HandlerCtx(ctx, HandlerFuncC(xhf))
|
|
}
|
|
|
|
// HandlerH is a helper to provide a standard http handler (http.HandlerFunc)
|
|
// to Handler(). Your final handler won't have access to the context though.
|
|
func (c Chain) HandlerH(h http.Handler) http.Handler {
|
|
ctx := context.Background()
|
|
return c.HandlerCtx(ctx, HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
|
|
h.ServeHTTP(w, r)
|
|
}))
|
|
}
|
|
|
|
// HandlerF is a helper to provide a standard http handler function
|
|
// (http.HandlerFunc) to Handler(). Your final handler won't have access
|
|
// to the context though.
|
|
func (c Chain) HandlerF(hf http.HandlerFunc) http.Handler {
|
|
ctx := context.Background()
|
|
return c.HandlerCtx(ctx, HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
|
|
hf(w, r)
|
|
}))
|
|
}
|
|
|
|
// HandlerCtx wraps the provided final handler with all the middleware appended to
|
|
// the chain and returns a new standard http.Handler instance.
|
|
func (c Chain) HandlerCtx(ctx context.Context, xh HandlerC) http.Handler {
|
|
return New(ctx, c.HandlerC(xh))
|
|
}
|
|
|
|
// HandlerC wraps the provided final handler with all the middleware appended to
|
|
// the chain and returns a HandlerC instance.
|
|
func (c Chain) HandlerC(xh HandlerC) HandlerC {
|
|
for i := len(c) - 1; i >= 0; i-- {
|
|
xh = c[i](xh)
|
|
}
|
|
return xh
|
|
}
|
|
|
|
// HandlerCF wraps the provided final handler func with all the middleware appended to
|
|
// the chain and returns a HandlerC instance.
|
|
//
|
|
// HandlerCF is equivalent to:
|
|
// c.HandlerC(xhandler.HandlerFuncC(xhc))
|
|
func (c Chain) HandlerCF(xhc HandlerFuncC) HandlerC {
|
|
return c.HandlerC(HandlerFuncC(xhc))
|
|
}
|