From 738d8ec00fad2e41b9f9483f0937b6f5cb7c2314 Mon Sep 17 00:00:00 2001 From: Nam Huynh Date: Mon, 5 Dec 2022 10:19:37 +0700 Subject: [PATCH] return nats instance to client --- constant.go | 20 ++++++++++++++++++++ natsio.go | 20 +++++--------------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/constant.go b/constant.go index 0d87a43..8937a91 100644 --- a/constant.go +++ b/constant.go @@ -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", +} diff --git a/natsio.go b/natsio.go index 169a675..3183d5e 100644 --- a/natsio.go +++ b/natsio.go @@ -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 }