-
Notifications
You must be signed in to change notification settings - Fork 5
/
options.go
79 lines (66 loc) · 1.43 KB
/
options.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package nats
import (
"context"
"strings"
"github.com/golang-queue/queue"
"github.com/golang-queue/queue/core"
"github.com/nats-io/nats.go"
)
// Option for queue system
type Option func(*options)
type options struct {
runFunc func(context.Context, core.QueuedMessage) error
logger queue.Logger
addr string
subj string
queue string
}
// WithAddr setup the addr of NATS
func WithAddr(addrs ...string) Option {
return func(w *options) {
if len(addrs) > 0 {
w.addr = strings.Join(addrs, ",")
}
}
}
// WithSubj setup the subject of NATS
func WithSubj(subj string) Option {
return func(w *options) {
w.subj = subj
}
}
// WithQueue setup the queue of NATS
func WithQueue(queue string) Option {
return func(w *options) {
w.queue = queue
}
}
// WithRunFunc setup the run func of queue
func WithRunFunc(fn func(context.Context, core.QueuedMessage) error) Option {
return func(w *options) {
w.runFunc = fn
}
}
// WithLogger set custom logger
func WithLogger(l queue.Logger) Option {
return func(w *options) {
w.logger = l
}
}
func newOptions(opts ...Option) options {
defaultOpts := options{
addr: nats.DefaultURL,
subj: "foobar",
queue: "foobar",
logger: queue.NewLogger(),
runFunc: func(context.Context, core.QueuedMessage) error {
return nil
},
}
// Loop through each option
for _, opt := range opts {
// Call the option giving the instantiated
opt(&defaultOpts)
}
return defaultOpts
}