-
Notifications
You must be signed in to change notification settings - Fork 1
/
pipeline.go
51 lines (42 loc) · 942 Bytes
/
pipeline.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
47
48
49
50
51
package xrest
import (
"net/http"
"golang.org/x/net/context"
)
type Pipeline struct {
handler Handler
plugs []Plugger
}
func (p *Pipeline) HTTPHandler() http.Handler {
var h Handler = p.handler
for i := len(p.plugs) - 1; i >= 0; i-- {
h = p.plugs[i].Plug(h)
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
h.ServeHTTP(ctx, w, r)
})
}
func (p *Pipeline) Handler() Handler {
var h Handler = p.handler
for i := len(p.plugs) - 1; i >= 0; i-- {
h = p.plugs[i].Plug(h)
}
return h
}
func (p *Pipeline) Plug(plugs ...Plugger) *Pipeline {
p.plugs = append(p.plugs, plugs...)
return p
}
func (p *Pipeline) Plugs() []Plugger {
return p.plugs
}
func (p *Pipeline) SetHandler(h Handler) *Pipeline {
p.handler = h
return p
}
func NewPipeline() *Pipeline {
return &Pipeline{
handler: HandlerFunc(func(context.Context, http.ResponseWriter, *http.Request) {}),
}
}