-
Notifications
You must be signed in to change notification settings - Fork 0
/
producer.go
93 lines (76 loc) · 1.88 KB
/
producer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/google/uuid"
"github.com/FaridehGhani/go-localstack/infra/cloud"
)
func main() {
sess := cloud.NewAWS()
sqsClient := sqs.New(sess)
dynamodbClient := dynamodb.New(sess)
ticker := time.NewTicker(3 * time.Second)
go func() {
for {
<-ticker.C
message := Message{
Id: generateUUID(),
Description: "new message",
CreatedAt: time.Now().Add(24 * time.Hour),
}
_, err := dynamodbClient.PutItem(
&dynamodb.PutItemInput{
TableName: aws.String("messages"),
Item: map[string]*dynamodb.AttributeValue{
"Id": {
S: aws.String(message.Id),
},
"Description": {
S: aws.String(message.Description),
},
"CreatedAt": {
S: aws.String(message.CreatedAt.Format(time.RFC3339)),
},
},
},
)
if err != nil {
log.Printf("failed to save message: %v", err)
}
if err = sendMessage(sqsClient, message); err != nil {
log.Printf("failed to send message: %v", err)
}
}
}()
select {}
}
type Message struct {
Id string `json:"Id"`
Description string `json:"Description"`
CreatedAt time.Time `json:"CreatedAt"`
}
func generateUUID() string {
id, err := uuid.NewRandom()
if err != nil {
return ""
}
return id.String()
}
func sendMessage(svc *sqs.SQS, item Message) error {
message := fmt.Sprintf(`{"id":"%s","description":"%s","dueDate":"%s"}`, item.Id, item.Description, item.CreatedAt)
input := &sqs.SendMessageInput{
DelaySeconds: aws.Int64(0),
MessageBody: &message,
QueueUrl: aws.String("http://localhost:4566/000000000000/my-queue"),
}
output, err := svc.SendMessage(input)
if err != nil {
return err
}
log.Printf("message sent with id %v", *output.MessageId)
return nil
}