Skip to content
Snippets Groups Projects
Select Git revision
  • df7b3695f8023f9cc1908ee7ff8734a56d702dfc
  • main default protected
  • v1.302.0
  • v1.301.0
  • v1.300.0
  • v1.299.0
  • v1.298.0
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
22 results

sqs.go

Blame
  • sqs.go 3.42 KiB
    package sqs
    
    /*Package sqs provides a simple interface to send messages to AWS SQS*/
    
    import (
    	"encoding/json"
    	"fmt"
    	"os"
    
    	"github.com/aws/aws-sdk-go/aws"
    	"github.com/aws/aws-sdk-go/aws/session"
    	"github.com/aws/aws-sdk-go/service/sqs"
    	"gitlab.com/uafrica/go-utils/logs"
    )
    
    // Messenger sends an arbitrary message via SQS
    type Messenger struct {
    	session  *session.Session
    	service  *sqs.SQS
    	queueURL string
    }
    
    // NewSQSMessenger constructs a Messenger which sends messages to an SQS queue
    // awsRegion - region that the queue was created
    // awsQueue - name of the queue
    // Note: Calling code needs SQS IAM permissions
    func NewSQSMessenger(awsRegion, queueUrl string) (*Messenger, error) {
    	// Make an AWS session
    	sess, err := session.NewSessionWithOptions(session.Options{
    		Config: aws.Config{
    			Region: aws.String(awsRegion),
    		},
    	})
    
    	if err != nil {
    		return nil, err
    	}
    
    	// Create SQS service
    	svc := sqs.New(sess)
    
    	return &Messenger{
    		session:  sess,
    		service:  svc,
    		queueURL: queueUrl,
    	}, nil
    }
    
    // SendSQSMessage sends a message to the queue associated with the messenger
    // headers - string message attributes of the SQS message (see AWS SQS documentation)
    // body - body of the SQS message (see AWS SQS documentation)
    func (m *Messenger) SendSQSMessage(headers map[string]string, body string, currentRequestID *string, sqsType string, headerKey string, messageGroupID string) (string, error) {
    	msgAttrs := make(map[string]*sqs.MessageAttributeValue)
    
    	for key, val := range headers {
    		msgAttrs[key] = &sqs.MessageAttributeValue{
    			DataType:    aws.String("String"),
    			StringValue: aws.String(val),
    		}
    	}
    
    	// Add request ID
    	if currentRequestID != nil {
    		msgAttrs[headerKey] = &sqs.MessageAttributeValue{
    			DataType:    aws.String("String"),
    			StringValue: aws.String(*currentRequestID),
    		}
    	}
    
    	msgAttrs["type"] = &sqs.MessageAttributeValue{