Merge pull request #1 from Selly-Modules/develop

update
This commit is contained in:
Minh Nguyen 2022-03-23 09:14:38 +07:00 committed by GitHub
commit cf44b8ef62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 113 additions and 0 deletions

1
.gitignore vendored
View File

@ -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/

52
appier.go Normal file
View File

@ -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
}

8
config.go Normal file
View File

@ -0,0 +1,8 @@
package appier
import "github.com/Selly-Modules/natsio"
// Config ...
type Config struct {
Nats natsio.Config
}

9
constant.go Normal file
View File

@ -0,0 +1,9 @@
package appier
const (
SubjectRequestProductUpsert = "selly.request.product.upsert"
)
const (
JetStreamAppierService = "Service_Appier"
)

14
go.mod Normal file
View File

@ -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
)

9
pull.go Normal file
View File

@ -0,0 +1,9 @@
package appier
// Pull ...
type Pull struct{}
// ProductUpsert ...
func (Pull) ProductUpsert(payload Payload) (bool, error) {
return publishWithJetStream(JetStreamAppierService, SubjectRequestProductUpsert, toBytes(payload))
}

11
struct.go Normal file
View File

@ -0,0 +1,11 @@
package appier
// RequestBody ...
type RequestBody struct {
Body []byte `json:"body"`
}
// Payload ...
type Payload struct {
Data []byte
}

9
util.go Normal file
View File

@ -0,0 +1,9 @@
package appier
import "encoding/json"
// toBytes ...
func toBytes(data interface{}) []byte {
b, _ := json.Marshal(data)
return b
}