-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
95 lines (74 loc) · 2.16 KB
/
main.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
94
95
package main
import (
"context"
"encoding/json"
"log"
"os"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/joho/godotenv"
"github.com/monitoring-consumer/configs"
"github.com/monitoring-consumer/handlers"
"github.com/monitoring-consumer/models"
)
func main() {
env := os.Getenv("ENVIRONMENT")
if env != "PRODUCTION" {
err := godotenv.Load()
if err != nil {
log.Fatal("Error on load .env file")
}
}
_, err := configs.Load()
if err != nil {
log.Fatalf("Error on load configs: %v", err)
}
processMessages()
}
func processMessages() {
cfg := configs.GetAWSConfig()
// configure aws session
config, err := config.LoadDefaultConfig(context.TODO())
config.Region = "sa-east-1"
if err != nil {
log.Fatalf("Error on create aws session: %v", err)
}
// Cria o cliente do SQS
sqsClient := sqs.NewFromConfig(config)
// Receber as mensagens da fila
for {
result, err := sqsClient.ReceiveMessage(context.Background(), &sqs.ReceiveMessageInput{
QueueUrl: aws.String(cfg.SQSQueueUrl),
MaxNumberOfMessages: *aws.Int32(10),
VisibilityTimeout: *aws.Int32(30),
WaitTimeSeconds: *aws.Int32(5),
})
if err != nil {
log.Printf("Error on receive messages: %v", err)
continue
}
// Processar as mensagens da fila
for _, message := range result.Messages {
// Converte o JSON para o Struct
var linkStruct models.Link
err := json.Unmarshal([]byte(*message.Body), &linkStruct)
if err != nil {
log.Printf("Error on decode mensagem: %v", err)
continue
}
responseTime, statusCode, statusText := handlers.VerifyUrl(linkStruct.Url)
// Salva no banco de dados a URL
handlers.SaveUrlResponse(linkStruct.Id, responseTime, statusCode, statusText)
handlers.UpdateUrl(linkStruct.Id, linkStruct.Link_execution, linkStruct.Execute_date)
// Deleta a mensagem da fila
_, err = sqsClient.DeleteMessage(context.Background(), &sqs.DeleteMessageInput{
QueueUrl: aws.String(cfg.SQSQueueUrl),
ReceiptHandle: message.ReceiptHandle,
})
if err != nil {
log.Printf("Error on delete message: %v", err)
}
}
}
}