return nats instance to client

This commit is contained in:
Nam Huynh 2022-12-05 10:19:37 +07:00
parent 11247a81c1
commit 738d8ec00f
2 changed files with 25 additions and 15 deletions

View File

@ -5,3 +5,23 @@ var Stream = struct {
}{
Selly: "selly",
}
var SellyServer = struct {
App string
Admin string
SMS string
}{
App: "app",
Admin: "admin",
SMS: "sms",
}
var SellyService = struct {
User string
HelpCenter string
Order string
}{
User: "user",
HelpCenter: "help_center",
Order: "order",
}

View File

@ -25,10 +25,10 @@ var (
)
// Connect ...
func Connect(cfg Config) error {
func Connect(cfg Config) (*Server, *JetStream, error) {
// validate
if err := cfg.validate(); err != nil {
return err
return nil, nil, err
}
// connect options
@ -48,7 +48,7 @@ func Connect(cfg Config) error {
nc, err := nats.Connect(cfg.URL, opts...)
if err != nil {
msg := fmt.Sprintf("error when connecting to NATS: %s", err.Error())
return errors.New(msg)
return nil, nil, errors.New(msg)
}
// set client
@ -58,7 +58,7 @@ func Connect(cfg Config) error {
js, err := nc.JetStream(nats.PublishAsyncMaxPending(256))
if err != nil {
msg := fmt.Sprintf("error when create NATS JetStream: %s", err.Error())
return errors.New(msg)
return nil, nil, errors.New(msg)
}
natsJetStream.instance = js
natsJetStream.streamName = cfg.StreamName
@ -68,15 +68,5 @@ func Connect(cfg Config) error {
fmt.Printf("⚡️[natsio]: connected to %s \n", cfg.URL)
return nil
}
// GetServer ...
func GetServer() Server {
return natsServer
}
// GetJetStream ...
func GetJetStream() JetStream {
return natsJetStream
return &natsServer, &natsJetStream, nil
}