go-sqs

module
v1.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2024 License: MIT

README ¶

Maintainability Test Coverage

Go SQS Consumer

🌟Description

This is a simple package to help you consume messages from AWS SQS.

🚀Features

  • Consume messages in parallel
  • Consume messages from different defined queues
  • Consume messages from different queues by a prefix
  • Error handling
  • Message unmarshalling
  • Message deletion
  • Logging

Installation

To install the package, use the following command:

go get github.com/inaciogu/go-sqs/consumer

Usage

package main

import (
	"fmt"

	sqsclient "github.com/inaciogu/go-sqs/consumer"
	"github.com/inaciogu/go-sqs/consumer/handler"
	"github.com/inaciogu/go-sqs/consumer/message"
	"github.com/joho/godotenv"
)

type Message struct {
	Name  string `json:"name"`
	Email string `json:"email"`
}

func main() {
	godotenv.Load(".env")

	consumer1 := sqsclient.New(nil, sqsclient.SQSClientOptions{
		QueueName: "test_queue",
		Handle: func(message *message.Message) bool {
			myMessage := Message{}

			// Unmarshal the message content
			err := message.Unmarshal(&myMessage)

			if err != nil {
				fmt.Println(err)

				// Do something if the message content cannot be unmarshalled
				return false
			}

			fmt.Println(myMessage.Email)

			return true
		},
		WaitTimeSeconds: 30,
		Region:                 "us-east-1",
	})

	consumer1.Start()
	// Or
	handler.New([]sqsclient.SQSClientInterface{
		consumer1,
	}).Run()
}

If you want to consume queues by a prefix, you can just set the PrefixBased option to true Then, the QueueName will be used as a prefix to find all queues that match the prefix.

Configuration

To give the package access to your AWS account, you can use the following environment variables:

AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY

Contribution

If you want to contribute to the development of this package, follow these steps:

  • Fork the repository
  • Create a new branch (git checkout -b feature/new-feature)
  • Commit your changes (git commit -m 'Add new feature')
  • Push to the branch (git push origin feature/new-feature)
  • Open a Pull Request

Running locally

To use this package locally (without using your own AWS account) you can execute the docker compose up command that will run the localstack and execute terraform commands to deploy the infra configured in /iac/terraform/main.tf locally.

License

This package is distributed under the MIT license. See the LICENSE file for more information.

Contact

Gustavo Inacio - Linkedin

Directories ¶

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL