appier/appier.go

60 lines
1.2 KiB
Go
Raw Normal View History

2022-03-23 02:12:54 +00:00
package appier
import (
"errors"
"fmt"
"github.com/Selly-Modules/natsio"
2022-03-24 10:04:34 +00:00
"github.com/Selly-Modules/redisdb"
2022-03-23 02:12:54 +00:00
)
// Client ...
type Client struct {
Config Config
natsServer natsio.Server
natsJetStream natsio.JetStream
2022-03-24 10:04:34 +00:00
Sync Sync
2022-03-23 03:05:15 +00:00
Pull Pull
2022-03-23 02:12:54 +00:00
}
2022-03-24 10:04:34 +00:00
var client *Client
2022-03-23 02:12:54 +00:00
// 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(),
}
2022-03-24 10:04:34 +00:00
// Connect redis
if err := redisdb.Connect(config.Redis.URI, config.Redis.Password); err != nil {
return nil, fmt.Errorf("redis connect failed: %v", err)
}
2022-03-25 07:03:17 +00:00
// Init schedule
initSchedule()
2022-03-23 02:12:54 +00:00
return client, nil
}
// publishWithJetStream ...
func publishWithJetStream(streamName, subject string, data []byte) (isPublished bool, err error) {
// Publish jet stream
2022-03-23 03:24:37 +00:00
if err = client.natsJetStream.Publish(streamName, subject, data); err != nil {
2022-03-23 02:12:54 +00:00
return
}
isPublished = true
return
}