-
Notifications
You must be signed in to change notification settings - Fork 2
/
group.go
49 lines (41 loc) · 1.43 KB
/
group.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
package gwc
import (
"context"
"github.com/delicb/cliware"
)
// Group is container of any additional middlewares that group of endpoints use.
type Group struct {
Next Doer
Chain *cliware.Chain
}
// NewGroup creates and returns new instance of Group that will apply all provided
// middlewares and use next Doer to do actual work.
func NewGroup(next Doer, middlewares ...cliware.Middleware) *Group {
return &Group{
Next: next,
Chain: cliware.NewChain(middlewares...),
}
}
// Exec is implementation of cliware.Middleware interface.
func (s *Group) Exec(handler cliware.Handler) cliware.Handler {
return s.Chain.Exec(handler)
}
// Use adds provided middlewares to this group's chain.
func (s *Group) Use(middleware ...cliware.Middleware) *Group {
s.Chain.Use(middleware...)
return s
}
// Do applies all middlewares from this layer and provided middlewares and
// calls next Doer to do actual work.
func (s *Group) Do(middlewares ...cliware.Middleware) (*Response, error) {
return s.DoCtx(context.Background(), middlewares...)
}
// DoCtx applies all middlewares from this layer and provided middlewares and
// calls next Doer to do actual work.
func (s *Group) DoCtx(ctx context.Context, middlewares ...cliware.Middleware) (*Response, error) {
// insert service itself to first place in middleware chain
middlewares = append(middlewares, nil)
copy(middlewares[1:], middlewares[0:])
middlewares[0] = s
return s.Next.DoCtx(ctx, middlewares...)
}