-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
128 lines (110 loc) · 3.47 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/didip/tollbooth"
"github.com/didip/tollbooth/limiter"
"github.com/getsentry/sentry-go"
"github.com/grantfayvor/hexcord-notifications/helpers"
"github.com/grantfayvor/hexcord-notifications/lib"
"github.com/grantfayvor/hexcord-notifications/lib/mailing"
"github.com/grantfayvor/hexcord-notifications/lib/messaging"
"github.com/grantfayvor/hexcord-notifications/lib/notification"
notifier "github.com/grantfayvor/hexcord-notifications/lib/notification"
"github.com/Kamva/mgm"
"github.com/joho/godotenv"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
err = sentry.Init(sentry.ClientOptions{
Dsn: os.Getenv("SENTRY_CLIENT"),
})
if err != nil {
log.Fatalf("sentry.Init: %s", err)
}
defer sentry.Flush(2 * time.Second)
if err := initDB(); err != nil {
log.Fatalf("An error occurred while trying to initialize the DB : %s", err)
}
go func() {
mReceiver := &messaging.Receiver{}
mReceiver.InitiateConnection()
mReceiver.Consume(os.Getenv("RABBIT_MQ_CONN_NOTIFICATION_QUEUE"), func(msg map[string]interface{}) {
firebase, err := (&helpers.Firebase{}).InitApp()
if err != nil {
log.Fatalf("An error occurred while initializing firebase app : %s", err)
}
received, err := json.Marshal(msg)
if err != nil {
log.Fatalf("An error occurred while marshalling the message : %s", err)
}
notification := ¬ification.Notification{}
err = json.Unmarshal(received, notification)
if err != nil {
log.Printf("An error occurred while parsing the json to notification object : %s", err)
return
}
notifier.SaveNotification(notification)
for _, recipient := range notification.GetRecipients() {
firebase.PushNotification(notification, recipient)
notificationMessage := notification.GetMessage()
mailTemplate := strings.ReplaceAll(
strings.Replace(
strings.Replace(
strings.Replace(
strings.Replace(mailing.RecordingNotificationMailTemplate, "{{recipientName}}", notificationMessage["recipientName"], 1),
"{{thumbNail}}",
notificationMessage["recordThumbnail"],
1,
),
"{{senderName}}",
notificationMessage["senderName"],
1,
),
"{{notificationMessage}}",
notificationMessage["title"],
1,
),
"{{recordingLink}}",
notificationMessage["recordingLink"],
)
err := mailing.NewMailer().
InitMessage(notificationMessage["recipientEmail"], notificationMessage["title"]).
SendMail(mailTemplate)
fmt.Println("==================")
fmt.Println(err)
}
})
}()
lmt := tollbooth.NewLimiter(20, &limiter.ExpirableOptions{DefaultExpirationTTL: time.Hour}).
SetIPLookups([]string{"X-Forwarded-For", "RemoteAddr", "X-Real-IP"}).
SetMethods([]string{"GET", "POST", "DELETE", "UPDATE"}).
SetTokenBucketExpirationTTL(time.Hour)
lib.InitializeRoutes(lmt)
fmt.Printf("Starting server at port %s\n", os.Getenv("PORT"))
if err := http.ListenAndServe(":"+os.Getenv("PORT"), nil); err != nil {
log.Fatal(err)
}
}
func initDB() error {
clientOptions := options.Client().ApplyURI(os.Getenv("MONGO_URI"))
err := mgm.SetDefaultConfig(nil, "screen_recorder", clientOptions)
if err != nil {
return err
}
client, err := mgm.NewClient(clientOptions)
if err != nil {
return err
}
return client.Ping(context.TODO(), nil)
}