diff --git a/.gitignore b/.gitignore index 66fd13c..33ce4c4 100644 --- a/.gitignore +++ b/.gitignore @@ -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/ diff --git a/appier.go b/appier.go new file mode 100644 index 0000000..e9c661b --- /dev/null +++ b/appier.go @@ -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 +} diff --git a/config.go b/config.go new file mode 100644 index 0000000..1788a48 --- /dev/null +++ b/config.go @@ -0,0 +1,8 @@ +package appier + +import "github.com/Selly-Modules/natsio" + +// Config ... +type Config struct { + Nats natsio.Config +} diff --git a/constant.go b/constant.go new file mode 100644 index 0000000..f796d14 --- /dev/null +++ b/constant.go @@ -0,0 +1,9 @@ +package appier + +const ( + SubjectRequestProductUpsert = "selly.request.product.upsert" +) + +const ( + JetStreamAppierService = "Service_Appier" +) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8c4c244 --- /dev/null +++ b/go.mod @@ -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 +) diff --git a/pull.go b/pull.go new file mode 100644 index 0000000..39acfed --- /dev/null +++ b/pull.go @@ -0,0 +1,9 @@ +package appier + +// Pull ... +type Pull struct{} + +// ProductUpsert ... +func (Pull) ProductUpsert(payload Payload) (bool, error) { + return publishWithJetStream(JetStreamAppierService, SubjectRequestProductUpsert, toBytes(payload)) +} diff --git a/struct.go b/struct.go new file mode 100644 index 0000000..189a2c4 --- /dev/null +++ b/struct.go @@ -0,0 +1,11 @@ +package appier + +// RequestBody ... +type RequestBody struct { + Body []byte `json:"body"` +} + +// Payload ... +type Payload struct { + Data []byte +} diff --git a/util.go b/util.go new file mode 100644 index 0000000..ae869d8 --- /dev/null +++ b/util.go @@ -0,0 +1,9 @@ +package appier + +import "encoding/json" + +// toBytes ... +func toBytes(data interface{}) []byte { + b, _ := json.Marshal(data) + return b +}