-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
44 lines (35 loc) · 998 Bytes
/
client.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
package windshift
import (
"fmt"
"log/slog"
"github.com/levelfourab/windshift-go/events"
eventsimpl "github.com/levelfourab/windshift-go/internal/events"
"github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
)
// NewEvents creates a new client for consuming and publishing events.
func NewEvents(nats *nats.Conn, opts ...ClientOption) (events.Client, error) {
options := &clientOptions{}
for _, o := range opts {
o(options)
}
if options.logger == nil {
options.logger = slog.Default()
}
js, err := jetstream.New(nats)
if err != nil {
return nil, fmt.Errorf("failed to create JetStream client: %w", err)
}
return eventsimpl.New(js, options.logger), nil
}
type clientOptions struct {
logger *slog.Logger
}
// ClientOption is an option to configure the client.
type ClientOption func(*clientOptions)
// WithLogger sets the logger for the client.
func WithLogger(logger *slog.Logger) ClientOption {
return func(o *clientOptions) {
o.logger = logger
}
}