build email

This commit is contained in:
Tue 2022-11-23 09:50:34 +07:00
parent 621b2f8c93
commit 4065ff14cc
8 changed files with 159 additions and 135 deletions

44
client.go Normal file
View File

@ -0,0 +1,44 @@
package email
import (
"errors"
"fmt"
"git.selly.red/Selly-Modules/natsio"
)
var (
c *Client
server natsio.Server
js natsio.JetStream
)
// GetClient ...
func GetClient() *Client {
return c
}
// Config ...
type Config struct {
Nats natsio.Config
}
// Client ...
type Client struct {
Config Config
}
// NewClient ...
func NewClient(cfg Config) (*Client, error) {
if cfg.Nats.URL == "" {
return nil, errors.New("nats url is required")
}
if err := natsio.Connect(cfg.Nats); err != nil {
return nil, fmt.Errorf("nats connect failed: %v", err)
}
c = &Client{
Config: cfg,
}
return c, nil
}

View File

@ -1,77 +0,0 @@
package email
import (
"encoding/json"
"errors"
"fmt"
"git.selly.red/Selly-Modules/natsio"
)
func getPrefixEmailService(subject string) string {
return fmt.Sprintf("selly.email.%s", subject)
}
const (
SubjectSendEmail = "send_email"
)
const (
KeyCreateSupplierUser = "create_supplier_user"
)
var (
c *Client
)
// GetEmail ...
func GetEmail() *Client {
return c
}
// Client ...
type Client struct {
Config Config
natsServer natsio.Server
natsJetStream natsio.JetStream
}
// NewClient ...
func NewClient(cfg Config) (*Client, error) {
if cfg.Nats.URL == "" {
return nil, errors.New("nats url is required")
}
if err := natsio.Connect(cfg.Nats); err != nil {
return nil, fmt.Errorf("nats connect failed: %v", err)
}
c = &Client{
Config: cfg,
natsServer: natsio.GetServer(),
natsJetStream: natsio.GetJetStream(),
}
return c, nil
}
func (c *Client) Send(payload SendPayload) (requestID string, err error) {
msg, err := c.natsServer.Request(getPrefixEmailService(SubjectSendEmail), toBytes(payload))
if err != nil {
return "", err
}
var res struct {
Data Response `json:"data"`
Error string `json:"error"`
}
if err = json.Unmarshal(msg.Data, &res); err != nil {
return "", err
}
if res.Error != "" {
return "", errors.New(res.Error)
}
return res.Data.RequestID, nil
}
func toBytes(data interface{}) []byte {
b, _ := json.Marshal(data)
return b
}

11
model/supplier_request.go Normal file
View File

@ -0,0 +1,11 @@
package model
type CreateSupplierUserEncrypt struct {
EncryptData string `json:"encryptData"`
}
type CreateSupplierUserRequest struct {
Account string `json:"account"`
Email string `json:"email"`
Password string `json:"password"`
}

View File

@ -0,0 +1,5 @@
package model
type CreateSupplierUserResponse struct {
RequestID string `json:"requestId"`
}

View File

@ -1,58 +0,0 @@
package email
import (
"git.selly.red/Selly-Modules/natsio"
)
// Config ...
type Config struct {
Nats natsio.Config
}
type To struct {
Name string `json:"name" bson:"name"`
Address string `json:"address" bson:"address"`
}
type From struct {
Name string `json:"name" bson:"name"`
Address string `json:"address" bson:"address"`
}
type Recipient struct {
To []To `json:"to" bson:"to"`
From From `json:"from" bson:"from"`
CC []string `json:"cc,omitempty" bson:"CC,omitempty"`
BCC []string `json:"bcc,omitempty" bson:"BCC,omitempty"`
Subject string `json:"subject,omitempty" bson:"subject,omitempty"`
Attachment []Attachment `json:"attachment,omitempty" bson:"attachment,omitempty"`
}
type Attachment struct {
URL string `json:"url"`
}
type Data struct {
Account string `json:"account" bson:"account,omitempty"`
Password string `json:"password" bson:"password,omitempty"`
}
// SendPayload ...
type SendPayload struct {
Recipient *Recipient `json:"recipient,omitempty" bson:"recipient,omitempty"`
Subject string `json:"subject" bson:"subject"`
Data *Data `json:"data,omitempty" bson:"data,omitempty"`
Key string `json:"key" bson:"key"`
TemplateID string `json:"templateId" bson:"templateId"`
Content *Content `json:"content,omitempty" bson:"content,omitempty"`
}
type Content struct {
Type string `json:"type" bson:"type"`
Value string `json:"value" bson:"value"`
}
// Response ...
type Response struct {
RequestID string `json:"requestId"`
}

32
supplier/supplier.go Normal file
View File

@ -0,0 +1,32 @@
package supplier
import (
"encoding/json"
"errors"
"git.selly.red/Selly-Modules/email/model"
"git.selly.red/Selly-Modules/email/utils"
"git.selly.red/Selly-Modules/natsio"
)
const (
SubjectCreateSupplierUser = "create_supplier_user"
)
func CreateSupplierUser(payload model.CreateSupplierUserEncrypt) (requestID string, err error) {
msg, err := natsio.GetServer().Request(SubjectCreateSupplierUser, utils.ToBytes(payload))
if err != nil {
return "", err
}
var res struct {
Data model.CreateSupplierUserResponse `json:"data"`
Error string `json:"error"`
}
if err = json.Unmarshal(msg.Data, &res); err != nil {
return "", err
}
if res.Error != "" {
return "", errors.New(res.Error)
}
return res.Data.RequestID, nil
}

8
utils/bytes.go Normal file
View File

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

59
utils/passphrase.go Normal file
View File

@ -0,0 +1,59 @@
package utils
import (
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/md5"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"io"
)
func GeneratePassphrase(key string) string {
hash := md5.New()
hash.Write([]byte(key))
return hex.EncodeToString(hash.Sum(nil))
}
func HashWithPassphrase(data []byte, key string) string {
h := hmac.New(sha256.New, []byte(key))
h.Write([]byte(data))
return hex.EncodeToString(h.Sum(nil))
}
func ASCEncrypt(data []byte, passphrase string) (string, error) {
block, _ := aes.NewCipher([]byte(GeneratePassphrase(passphrase)))
gcm, err := cipher.NewGCM(block)
if err != nil {
return "", err
}
nonce := make([]byte, gcm.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
return "", err
}
ciphertext := gcm.Seal(nonce, nonce, data, nil)
return base64.StdEncoding.EncodeToString(ciphertext), nil
}
func ASCDecrypt(msg, passphrase string) (string, error) {
data, err := base64.StdEncoding.DecodeString(msg)
key := []byte(GeneratePassphrase(passphrase))
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return "", err
}
nonceSize := gcm.NonceSize()
nonce, ciphertext := data[:nonceSize], data[nonceSize:]
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return "", err
}
return string(plaintext), nil
}