-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect.go
60 lines (52 loc) · 1.51 KB
/
connect.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
package nats
import (
"log"
"github.com/nats-io/nats.go"
)
func ConnectWithNATS(servers, clientCert, clientKey, caCert string) (*nats.Conn, error) {
c, err := nats.Connect(
servers,
nats.RootCAs(caCert),
nats.ClientCert(clientCert, clientKey),
nats.MaxReconnects(-1),
nats.ReconnectHandler(func(nc *nats.Conn) {
log.Println("[INFO]: reconnected to the message broker")
}),
nats.DisconnectErrHandler(func(nc *nats.Conn, err error) {
if err != nil {
log.Printf("[INFO]: disconnected from message broker due to: %s, will attempt reconnect", err.Error())
}
}),
nats.ClosedHandler(func(nc *nats.Conn) {
log.Printf("[INFO]: connection closed. Reason: %q\n", nc.LastError())
}),
)
if err != nil {
return nil, err
}
log.Println("[INFO]: connection established with NATS server")
return c, nil
}
func ConnectWithNATSTOKEN(servers, token string) (*nats.Conn, error) {
c, err := nats.Connect(
servers,
nats.Token(token),
nats.MaxReconnects(-1),
nats.ReconnectHandler(func(nc *nats.Conn) {
log.Println("[INFO]: reconnected to the message broker")
}),
nats.DisconnectErrHandler(func(nc *nats.Conn, err error) {
if err != nil {
log.Printf("[INFO]: disconnected from message broker due to: %s, will attempt reconnect", err.Error())
}
}),
nats.ClosedHandler(func(nc *nats.Conn) {
log.Printf("[INFO]: connection closed. Reason: %q\n", nc.LastError())
}),
)
if err != nil {
return nil, err
}
log.Println("[INFO]: connection established with NATS server")
return c, nil
}