Repository navigation
Expand file tree
/
Copy pathmiddleware.go
More file actions
52 lines (41 loc) · 1.54 KB
/
Copy pathmiddleware.go
File metadata and controls
52 lines (41 loc) · 1.54 KB
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
package gopherqueue
import "context"
// Handler defines the interface for executing a Job.
type Handler interface {
ProcessJob(ctx context.Context, job *Job) error
}
// HandlerFunc is an adapter type allowing ordinary functions to act as a Handler.
type HandlerFunc func(ctx context.Context, job *Job) error
// ProcessJob calls the underlying HandlerFunc.
func (f HandlerFunc) ProcessJob(ctx context.Context, job *Job) error {
return f(ctx, job)
}
// MiddlewareFunc defines a middleware function wrapper around a Handler.
type MiddlewareFunc func(next Handler) Handler
// Chain manages an ordered slice of MiddlewareFunc and compiles them into a single Handler.
type Chain struct {
middlewares []MiddlewareFunc
}
// NewChain creates a new Chain instance with the provided middlewares.
func NewChain(middlewares ...MiddlewareFunc) Chain {
return Chain{middlewares: append([]MiddlewareFunc(nil), middlewares...)}
}
// Then links the middleware chain to a final Job Handler.
func (c Chain) Then(final Handler) Handler {
if final == nil {
final = HandlerFunc(func(ctx context.Context, job *Job) error {
return nil
})
}
for i := len(c.middlewares) - 1; i >= 0; i-- {
final = c.middlewares[i](final)
}
return final
}
// Append creates a new Chain by appending additional middlewares to the existing chain.
func (c Chain) Append(middlewares ...MiddlewareFunc) Chain {
newMw := make([]MiddlewareFunc, 0, len(c.middlewares)+len(middlewares))
newMw = append(newMw, c.middlewares...)
newMw = append(newMw, middlewares...)
return Chain{middlewares: newMw}
}