This repository has been archived on 2023-07-11. You can view files and clone it, but cannot push or open issues or pull requests.
nats-cli/nats.go

52 lines
1,004 B
Go
Raw Normal View History

2017-11-29 14:37:37 -05:00
package nats_cli
import (
"errors"
2018-01-16 14:21:35 -05:00
ctls "crypto/tls"
2017-11-29 14:37:37 -05:00
"github.com/nats-io/nats"
)
2018-01-16 14:21:35 -05:00
func Connect(url string, tls bool, certPath string, keyPath string, caCertPath string, verify bool) (*nats.Conn, error) {
2017-11-29 14:37:37 -05:00
if tls {
if len(certPath) == 0 {
return nil, errors.New("tlscert not set")
}
if len(keyPath) == 0 {
return nil, errors.New("tlskey not set")
}
cert := nats.ClientCert(certPath, keyPath)
var conn *nats.Conn
var err error
2018-01-16 14:21:35 -05:00
tlsverify := func(o *nats.Options) error {
2018-01-16 15:25:12 -05:00
if o.TLSConfig != nil {
2018-01-16 14:21:35 -05:00
o.TLSConfig.InsecureSkipVerify = !verify
} else {
o.TLSConfig = &ctls.Config{InsecureSkipVerify: !verify}
}
return nil
}
2017-11-29 14:37:37 -05:00
if len(caCertPath) > 0 {
2018-01-16 14:21:35 -05:00
conn, err = nats.Connect(url, nats.RootCAs(caCertPath), cert, tlsverify)
2017-11-29 14:37:37 -05:00
} else {
2018-01-16 14:21:35 -05:00
conn, err = nats.Connect(url, cert, tlsverify)
2017-11-29 14:37:37 -05:00
}
if err != nil {
return nil, err
}
return conn, nil
} else {
conn, err := nats.Connect(url)
if err != nil {
return nil, err
}
return conn, nil
}
}