-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchain.go
More file actions
28 lines (22 loc) · 695 Bytes
/
Copy pathchain.go
File metadata and controls
28 lines (22 loc) · 695 Bytes
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
//Package chain aims to implement a chain of handlers to use with the http package in the standard library
package chain
import "net/http"
type Handler func(http.Handler) http.Handler
//HandlerChain contains a chain of http.Handlers to use
type HandlerChain struct {
chain []Handler
}
//New appends given handlers to a new chain
func New(h ...Handler) HandlerChain {
return HandlerChain{append(([]Handler)(nil), h...)}
}
//Final wraps the handlers into the others, and lastly the given handler f to be used
func (h HandlerChain) Final(f http.Handler) http.Handler {
if f == nil {
f = http.DefaultServeMux
}
for i := range h.chain {
f = h.chain[len(h.chain)-1-i](f)
}
return f
}