commit
						cf44b8ef62
					
				| 
						 | 
				
			
			@ -10,6 +10,7 @@
 | 
			
		|||
 | 
			
		||||
# Output of the go coverage tool, specifically when used with LiteIDE
 | 
			
		||||
*.out
 | 
			
		||||
.idea
 | 
			
		||||
 | 
			
		||||
# Dependency directories (remove the comment below to include it)
 | 
			
		||||
# vendor/
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,52 @@
 | 
			
		|||
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
 | 
			
		||||
	if err := client.natsJetStream.Publish(streamName, subject, toBytes(req)); err != nil {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	isPublished = true
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,8 @@
 | 
			
		|||
package appier
 | 
			
		||||
 | 
			
		||||
import "github.com/Selly-Modules/natsio"
 | 
			
		||||
 | 
			
		||||
// Config ...
 | 
			
		||||
type Config struct {
 | 
			
		||||
	Nats natsio.Config
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,9 @@
 | 
			
		|||
package appier
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	SubjectRequestProductUpsert = "selly.request.product.upsert"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	JetStreamAppierService = "Service_Appier"
 | 
			
		||||
)
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,14 @@
 | 
			
		|||
module github.com/Selly-Modules/appier
 | 
			
		||||
 | 
			
		||||
go 1.17
 | 
			
		||||
 | 
			
		||||
require github.com/Selly-Modules/natsio v0.0.0-20220321031929-3fe4271f1bbc
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	github.com/logrusorgru/aurora v2.0.3+incompatible // indirect
 | 
			
		||||
	github.com/nats-io/nats.go v1.13.0 // indirect
 | 
			
		||||
	github.com/nats-io/nkeys v0.3.0 // indirect
 | 
			
		||||
	github.com/nats-io/nuid v1.0.1 // indirect
 | 
			
		||||
	github.com/thoas/go-funk v0.9.2 // indirect
 | 
			
		||||
	golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd // indirect
 | 
			
		||||
)
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,9 @@
 | 
			
		|||
package appier
 | 
			
		||||
 | 
			
		||||
// Pull ...
 | 
			
		||||
type Pull struct{}
 | 
			
		||||
 | 
			
		||||
// ProductUpsert ...
 | 
			
		||||
func (Pull) ProductUpsert(payload Payload) (bool, error) {
 | 
			
		||||
	return publishWithJetStream(JetStreamAppierService, SubjectRequestProductUpsert, toBytes(payload))
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,11 @@
 | 
			
		|||
package appier
 | 
			
		||||
 | 
			
		||||
// RequestBody ...
 | 
			
		||||
type RequestBody struct {
 | 
			
		||||
	Body []byte `json:"body"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Payload ...
 | 
			
		||||
type Payload struct {
 | 
			
		||||
	Data []byte
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue