2022-03-10 04:01:06 +00:00
|
|
|
package elasticsearch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/Selly-Modules/natsio"
|
|
|
|
)
|
|
|
|
|
2022-03-10 04:36:06 +00:00
|
|
|
// Client ...
|
2022-03-10 04:01:06 +00:00
|
|
|
type Client struct {
|
|
|
|
Config Config
|
|
|
|
natsServer natsio.Server
|
|
|
|
natsJetStream natsio.JetStream
|
2022-03-19 04:59:43 +00:00
|
|
|
Queue Queue
|
|
|
|
Pull Pull
|
|
|
|
Request Request
|
|
|
|
Push Push
|
2022-03-10 04:01:06 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 04:59:43 +00:00
|
|
|
var (
|
|
|
|
client *Client
|
|
|
|
)
|
|
|
|
|
2022-03-10 04:36:06 +00:00
|
|
|
// NewClient
|
|
|
|
// Init client elasticsearch
|
2022-03-10 04:01:06 +00:00
|
|
|
func NewClient(config Config) (*Client, error) {
|
|
|
|
if config.ApiKey == "" {
|
|
|
|
return nil, errors.New("api key is required")
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-03-19 04:59:43 +00:00
|
|
|
client = &Client{
|
2022-03-10 04:01:06 +00:00
|
|
|
Config: config,
|
|
|
|
natsServer: natsio.GetServer(),
|
|
|
|
natsJetStream: natsio.GetJetStream(),
|
2022-03-19 04:59:43 +00:00
|
|
|
Queue: Queue{},
|
|
|
|
Pull: Pull{},
|
|
|
|
Request: Request{},
|
|
|
|
Push: Push{},
|
2022-03-10 04:01:06 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 04:59:43 +00:00
|
|
|
return client, nil
|
2022-03-18 05:36:36 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 04:59:43 +00:00
|
|
|
func GetClient() *Client {
|
|
|
|
return client
|
2022-03-10 04:01:06 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 04:59:43 +00:00
|
|
|
// RequestNats
|
|
|
|
// publish message to nats and waiting response
|
|
|
|
func (c *Client) RequestNats(subject string, data []byte) (*Response, error) {
|
2022-03-10 04:01:06 +00:00
|
|
|
var (
|
2022-03-10 13:41:44 +00:00
|
|
|
req = RequestBody{
|
|
|
|
ApiKey: c.Config.ApiKey,
|
2022-03-19 04:59:43 +00:00
|
|
|
Body: data,
|
2022-03-10 13:41:44 +00:00
|
|
|
}
|
|
|
|
res *Response
|
2022-03-10 04:01:06 +00:00
|
|
|
)
|
2022-03-19 04:59:43 +00:00
|
|
|
msg, err := c.natsServer.Request(subject, toBytes(req))
|
2022-03-10 04:01:06 +00:00
|
|
|
if err != nil {
|
2022-03-10 13:41:44 +00:00
|
|
|
return nil, err
|
2022-03-10 04:01:06 +00:00
|
|
|
}
|
|
|
|
if err = json.Unmarshal(msg.Data, &res); err != nil {
|
2022-03-10 13:41:44 +00:00
|
|
|
return nil, err
|
2022-03-10 04:01:06 +00:00
|
|
|
}
|
|
|
|
if res.Message != "" {
|
2022-03-10 13:41:44 +00:00
|
|
|
return nil, errors.New(res.Message)
|
2022-03-10 04:01:06 +00:00
|
|
|
}
|
2022-03-10 13:41:44 +00:00
|
|
|
return res, nil
|
2022-03-10 04:01:06 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 04:59:43 +00:00
|
|
|
// PublishWithJetStream
|
|
|
|
// Sync data to services ES with JetStream
|
|
|
|
func (c *Client) PublishWithJetStream(streamName, subject string, data []byte) (bool, error) {
|
2022-03-10 04:01:06 +00:00
|
|
|
var (
|
2022-03-10 13:41:44 +00:00
|
|
|
req = RequestBody{
|
|
|
|
ApiKey: c.Config.ApiKey,
|
2022-03-19 04:59:43 +00:00
|
|
|
Body: data,
|
2022-03-10 13:41:44 +00:00
|
|
|
}
|
2022-03-10 04:01:06 +00:00
|
|
|
)
|
2022-03-19 04:59:43 +00:00
|
|
|
err := c.natsJetStream.Publish(streamName, subject, toBytes(req))
|
2022-03-10 04:01:06 +00:00
|
|
|
if err != nil {
|
2022-03-10 13:41:44 +00:00
|
|
|
return false, err
|
2022-03-10 04:01:06 +00:00
|
|
|
}
|
2022-03-15 08:00:34 +00:00
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-03-10 04:01:06 +00:00
|
|
|
func toBytes(data interface{}) []byte {
|
|
|
|
b, _ := json.Marshal(data)
|
|
|
|
return b
|
|
|
|
}
|