-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.go
56 lines (45 loc) · 949 Bytes
/
run.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
52
53
54
55
56
package wenex
import (
"context"
"net/http"
)
// GetRun return wenex.Run from context for current handler.
// It used to manage current handlers chain.
func GetRun(ctx context.Context) *Run {
runInterface := ctx.Value(ctxRun)
if runInterface == nil {
return nil
}
if run, ok := runInterface.(*Run); ok {
return run
}
return nil
}
func newRun(w http.ResponseWriter, r *http.Request, handler []http.Handler) *Run {
run := &Run{
rWriter: w,
handler: handler,
}
run.request = r.WithContext(context.WithValue(r.Context(), ctxRun, run))
return run
}
// Run struct
type Run struct {
rWriter http.ResponseWriter
request *http.Request
handler []http.Handler
}
// Next method
func (r *Run) Next() bool {
if len(r.handler) == 0 {
return false
}
handler := r.handler[0]
r.handler = r.handler[1:]
handler.ServeHTTP(r.rWriter, r.request)
return true
}
// Break method
func (r *Run) Break() {
r.handler = r.handler[:0]
}