-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (72 loc) · 1.68 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
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/jasonlvhit/gocron"
"github.com/joho/godotenv"
"github.com/mailgun/mailgun-go"
"github.com/streadway/amqp"
)
func sendSimpleMessage() {
api_key := os.Getenv("MAILGUN_API_KEY")
sandbox_domain := os.Getenv("MAILGUN_DOMAIN")
mg := mailgun.NewMailgun(sandbox_domain, api_key)
sender := "testingsomething@example.com"
subject := "security warning"
body := "Your fallback api is exposed"
recipient := "sunguralican@gmail.com"
message := mg.NewMessage(sender, subject, body, recipient)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
resp, id, err := mg.Send(ctx, message)
if err != nil {
log.Fatal(err)
}
fmt.Printf("ID: %s Resp: %s\n", id, resp)
}
func processMessages() {
amqpServerURL := os.Getenv("AMQP_SERVER_URL")
connectRabbitMQ, err := amqp.Dial(amqpServerURL)
if err != nil {
panic(err)
}
defer connectRabbitMQ.Close()
channelRabbitMQ, err := connectRabbitMQ.Channel()
if err != nil {
panic(err)
}
defer channelRabbitMQ.Close()
messages, err := channelRabbitMQ.Consume(
"FallbackAPIQueue",
"",
true,
false,
false,
false,
nil,
)
if err != nil {
log.Println(err)
}
log.Println("You've connected to RabbitMQ")
log.Println("Waiting for messages")
if len(messages) == 0 {
log.Println("You don't have any messages in the queue")
}
for message := range messages {
if string(message.Body) == "A request has been sent via fallback API" {
sendSimpleMessage()
}
}
}
func main() {
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Error loading .env file")
}
gocron.Every(2).Minutes().Do(processMessages)
<-gocron.Start()
}