handler.go 873 B

123456789101112131415161718192021222324252627282930313233
  1. package main
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "time"
  7. "github.com/aws/aws-sdk-go-v2/service/sqs/types"
  8. "github.com/google/uuid"
  9. )
  10. // HandleDummy is blissfully unaware of SQS, and could therefore be used as a handler for another message queue.
  11. func HandleDummy(_ context.Context, enc *json.Encoder,
  12. msgID uuid.UUID, sentTime time.Time, msg []byte, meta map[string]types.MessageAttributeValue) error {
  13. type EventBody struct {
  14. EventID uuid.UUID `json:"event_id"`
  15. EventTime time.Time `json:"event_time"`
  16. Data map[string]int `json:"data,omitempty"`
  17. Meta map[string]types.MessageAttributeValue
  18. }
  19. evt := EventBody{
  20. EventID: msgID,
  21. EventTime: sentTime,
  22. Meta: meta,
  23. }
  24. if err := json.Unmarshal(msg, &evt.Data); err != nil {
  25. return fmt.Errorf("error parsing body as JSON: %w", err)
  26. }
  27. enc.Encode(evt)
  28. return nil
  29. }