-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathinjector_chain.go
46 lines (37 loc) · 1.17 KB
/
injector_chain.go
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package fault
import "net/http"
// ChainInjector combines many Injectors into a single Injector that runs them in order.
type ChainInjector struct {
middlewares []func(next http.Handler) http.Handler
}
// ChainInjectorOption configures a ChainInjector.
type ChainInjectorOption interface {
applyChainInjector(i *ChainInjector) error
}
// NewChainInjector combines many Injectors into a single Injector that runs them in order.
func NewChainInjector(is []Injector, opts ...ChainInjectorOption) (*ChainInjector, error) {
// set defaults
ci := &ChainInjector{}
// apply options
for _, opt := range opts {
err := opt.applyChainInjector(ci)
if err != nil {
return nil, err
}
}
// set middleware
for _, i := range is {
ci.middlewares = append(ci.middlewares, i.Handler)
}
return ci, nil
}
// Handler executes ChainInjector.middlewares in order and then returns.
func (i *ChainInjector) Handler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Loop in reverse to preserve handler order
for idx := len(i.middlewares) - 1; idx >= 0; idx-- {
next = i.middlewares[idx](next)
}
next.ServeHTTP(w, r)
})
}