Skip to content

Commit

Permalink
Merge pull request justinas#8 from juztin/master
Browse files Browse the repository at this point in the history
Added a ThenFunc, just to avoid http.HandlerFunc calls.
  • Loading branch information
justinas committed Jul 20, 2014
2 parents 5c78182 + 5baf100 commit 5012203
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ func (c Chain) Then(h http.Handler) http.Handler {
return final
}

// Then chains the middleware and returns the final http.Handler.
// New(m1, m2, m3).ThenFunc(h)
// is equivalent to:
// m1(m2(m3(http.HandlerFunc(h))))
// When the request comes in, it will be passed to m1, then m2, then m3
// and finally, the given handler
// (assuming every middleware calls the following one).
//
// A chain can be safely reused by calling ThenFunc() several times.
// stdStack := alice.New(ratelimitHandler, csrfHandler)
// indexPipe = stdStack.ThenFunc(indexHandlerFunc)
// authPipe = stdStack.ThenFunc(authHandlerFunc)
// Note that constructors are called on every call to Then()
// and thus several instances of the same middleware will be created
// when a chain is reused in this way.
// For proper middleware, this should cause no problems.
//
// ThenFunc() treats nil as http.DefaultServeMux.
func (c Chain) ThenFunc(fn http.HandlerFunc) http.Handler {
return c.Then(http.HandlerFunc(fn))
}

// Append extends a chain, adding the specified constructors
// as the last ones in the request flow.
//
Expand Down

0 comments on commit 5012203

Please sign in to comment.