2022-03-23 02:12:54 +00:00
|
|
|
package appier
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/Selly-Modules/natsio"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Client ...
|
|
|
|
type Client struct {
|
|
|
|
Config Config
|
|
|
|
natsServer natsio.Server
|
|
|
|
natsJetStream natsio.JetStream
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
client *Client
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewClient
|
|
|
|
// Init client ...
|
|
|
|
func NewClient(config Config) (*Client, error) {
|
|
|
|
if config.Nats.URL == "" {
|
|
|
|
return nil, errors.New("nats url is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := natsio.Connect(config.Nats); err != nil {
|
|
|
|
return nil, fmt.Errorf("nats connect failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client = &Client{
|
|
|
|
Config: config,
|
|
|
|
natsServer: natsio.GetServer(),
|
|
|
|
natsJetStream: natsio.GetJetStream(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// publishWithJetStream ...
|
|
|
|
func publishWithJetStream(streamName, subject string, data []byte) (isPublished bool, err error) {
|
|
|
|
var req = RequestBody{Body: data}
|
|
|
|
|
|
|
|
// Publish jet stream
|
2022-03-23 02:44:57 +00:00
|
|
|
if err = client.natsJetStream.Publish(streamName, subject, toBytes(req)); err != nil {
|
2022-03-23 02:12:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
isPublished = true
|
|
|
|
return
|
|
|
|
}
|