diff --git a/chain.go b/chain.go index 88e6846..a227841 100644 --- a/chain.go +++ b/chain.go @@ -10,21 +10,21 @@ func Chain(middlewares ...func(http.Handler) http.Handler) Middlewares { // Handler builds and returns a http.Handler from the chain of middlewares, // with `h http.Handler` as the final handler. func (mws Middlewares) Handler(h http.Handler) http.Handler { - return &ChainHandler{mws, h, chain(mws, h)} + return &ChainHandler{h, chain(mws, h), mws} } // HandlerFunc builds and returns a http.Handler from the chain of middlewares, // with `h http.Handler` as the final handler. func (mws Middlewares) HandlerFunc(h http.HandlerFunc) http.Handler { - return &ChainHandler{mws, h, chain(mws, h)} + return &ChainHandler{h, chain(mws, h), mws} } // ChainHandler is a http.Handler with support for handler composition and // execution. type ChainHandler struct { - Middlewares Middlewares Endpoint http.Handler chain http.Handler + Middlewares Middlewares } func (c *ChainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { diff --git a/context.go b/context.go index 8c97f21..814c263 100644 --- a/context.go +++ b/context.go @@ -45,37 +45,37 @@ var ( type Context struct { Routes Routes + // parentCtx is the parent of this one, for using Context as a + // context.Context directly. This is an optimization that saves + // 1 allocation. + parentCtx context.Context + // Routing path/method override used during the route search. // See Mux#routeHTTP method. RoutePath string RouteMethod string - // Routing pattern stack throughout the lifecycle of the request, - // across all connected routers. It is a record of all matching - // patterns across a stack of sub-routers. - RoutePatterns []string - // URLParams are the stack of routeParams captured during the // routing lifecycle across a stack of sub-routers. URLParams RouteParams + // Route parameters matched for the current sub-router. It is + // intentionally unexported so it cant be tampered. + routeParams RouteParams + // The endpoint routing pattern that matched the request URI path // or `RoutePath` of the current sub-router. This value will update // during the lifecycle of a request passing through a stack of // sub-routers. routePattern string - // Route parameters matched for the current sub-router. It is - // intentionally unexported so it cant be tampered. - routeParams RouteParams + // Routing pattern stack throughout the lifecycle of the request, + // across all connected routers. It is a record of all matching + // patterns across a stack of sub-routers. + RoutePatterns []string // methodNotAllowed hint methodNotAllowed bool - - // parentCtx is the parent of this one, for using Context as a - // context.Context directly. This is an optimization that saves - // 1 allocation. - parentCtx context.Context } // Reset a routing context to its initial state. diff --git a/middleware/throttle.go b/middleware/throttle.go index 7bb2e80..8dcb944 100644 --- a/middleware/throttle.go +++ b/middleware/throttle.go @@ -18,10 +18,10 @@ var ( // ThrottleOpts represents a set of throttling options. type ThrottleOpts struct { + RetryAfterFn func(ctxDone bool) time.Duration Limit int BacklogLimit int BacklogTimeout time.Duration - RetryAfterFn func(ctxDone bool) time.Duration } // Throttle is a middleware that limits number of currently processed requests @@ -119,8 +119,8 @@ type token struct{} type throttler struct { tokens chan token backlogTokens chan token - backlogTimeout time.Duration retryAfterFn func(ctxDone bool) time.Duration + backlogTimeout time.Duration } // setRetryAfterHeaderIfNeeded sets Retry-After HTTP header if corresponding retryAfterFn option of throttler is initialized. diff --git a/middleware/wrap_writer.go b/middleware/wrap_writer.go index 6438c7a..588887b 100644 --- a/middleware/wrap_writer.go +++ b/middleware/wrap_writer.go @@ -57,10 +57,10 @@ type WrapResponseWriter interface { // http.ResponseWriter interface. type basicWriter struct { http.ResponseWriter - wroteHeader bool + tee io.Writer code int bytes int - tee io.Writer + wroteHeader bool } func (b *basicWriter) WriteHeader(code int) { diff --git a/tree.go b/tree.go index 8057c52..03f40f9 100644 --- a/tree.go +++ b/tree.go @@ -631,7 +631,7 @@ func (n *node) routes() []Route { hs[m] = h.handler } - rt := Route{p, hs, subroutes} + rt := Route{subroutes, hs, p} rts = append(rts, rt) } @@ -815,9 +815,9 @@ func (ns nodes) findEdge(label byte) *node { // Route describes the details of a routing handler. // Handlers map key is an HTTP method type Route struct { - Pattern string - Handlers map[string]http.Handler SubRoutes Routes + Handlers map[string]http.Handler + Pattern string } // WalkFunc is the type of the function called for each method and route visited by Walk.