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