-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
401 lines (308 loc) · 11.2 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package main
import (
"calenvite/models"
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"reflect"
"strconv"
"strings"
"time"
ics "github.com/arran4/golang-ical"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/mailgun/mailgun-go/v4"
"gopkg.in/go-playground/validator.v9"
gomail "gopkg.in/gomail.v2"
)
var settings = models.Settings{
Mailgun: models.Mailgun{
Domain: os.Getenv("CALENVITE_SVC_MAILGUN_DOMAIN"),
SecretKey: os.Getenv("CALENVITE_SVC_MAILGUN_KEY"),
},
SMTP: models.SMTP{
Host: os.Getenv("CALENVITE_SVC_SMTP_HOST"),
Port: os.Getenv("CALENVITE_SVC_SMTP_PORT"),
User: os.Getenv("CALENVITE_SVC_SMTP_USER"),
Password: os.Getenv("CALENVITE_SVC_SMTP_PASSWORD"),
},
SenderAddress: os.Getenv("CALENVITE_SVC_EMAIL_SENDER_ADDRESS"),
SendUsing: os.Getenv("CALENVITE_SVC_SEND_USING"),
ServicePort: os.Getenv("CALENVITE_SVC_PORT"),
}
var API500Error = models.APIResponse{
Message: "ERROR",
StatusCode: http.StatusInternalServerError,
ErrorFields: nil,
}
// use a single instance of Validate, it caches struct info
var validate *validator.Validate
func HealthcheckHandler(c echo.Context) error {
if value, ok := os.LookupEnv("CALENVITE_SVC_SEND_USING"); ok {
if value != "MAILGUN" && value != "SMTP" {
log.Printf("Env var CALENVITE_SVC_SEND_USING value invalid: %s. Valid Values: MAILGUN or SMTP. Check documentation\n", value)
return c.JSON(http.StatusInternalServerError, nil)
}
} else {
log.Println("Env var CALENVITE_SVC_SEND_USING not set. Check documentation")
return c.JSON(http.StatusInternalServerError, nil)
}
if value, ok := os.LookupEnv("CALENVITE_SVC_SEND_USING"); ok {
if value == "MAILGUN" {
if _, ok := os.LookupEnv("CALENVITE_SVC_MAILGUN_DOMAIN"); !ok {
log.Println("Env var CALENVITE_SVC_MAILGUN_DOMAIN not set. Check documentation")
return c.JSON(http.StatusInternalServerError, nil)
}
if _, ok := os.LookupEnv("CALENVITE_SVC_MAILGUN_KEY"); !ok {
log.Println("Env var CALENVITE_SVC_MAILGUN_KEY not set. Check documentation")
return c.JSON(http.StatusInternalServerError, nil)
}
} else if value == "SMTP" {
if _, ok := os.LookupEnv("CALENVITE_SVC_SMTP_HOST"); !ok {
log.Println("Env var CALENVITE_SVC_SMTP_HOST not set. Check documentation")
return c.JSON(http.StatusInternalServerError, nil)
}
if _, ok := os.LookupEnv("CALENVITE_SVC_SMTP_USER"); !ok {
log.Println("Env var CALENVITE_SVC_SMTP_USER not set. Check documentation")
return c.JSON(http.StatusInternalServerError, nil)
}
if _, ok := os.LookupEnv("CALENVITE_SVC_SMTP_PASSWORD"); !ok {
log.Println("Env var CALENVITE_SVC_SMTP_PASSWORD not set. Check documentation")
return c.JSON(http.StatusInternalServerError, nil)
}
if _, ok := os.LookupEnv("CALENVITE_SVC_SMTP_PORT"); !ok {
log.Println("Env var CALENVITE_SVC_SMTP_PORT not set. Check documentation")
return c.JSON(http.StatusInternalServerError, nil)
}
}
}
if _, ok := os.LookupEnv("CALENVITE_SVC_EMAIL_SENDER_ADDRESS"); !ok {
log.Println("Env var CALENVITE_SVC_EMAIL_SENDER_ADDRESS is not set. Check documentation")
return c.JSON(http.StatusInternalServerError, nil)
}
return c.JSON(http.StatusOK, nil)
}
func InviteHandler(c echo.Context) error {
payload := new(models.RequestPayload)
if err := c.Bind(payload); err != nil {
return err
}
// validate payload
validate = validator.New()
// register function to get tag name from json tags.
validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
if name == "-" {
return ""
}
return name
})
err := validate.Struct(payload)
if err != nil {
var fieldErr = []models.ErrorFields{}
for _, err := range err.(validator.ValidationErrors) {
fieldErr = append(fieldErr, models.ErrorFields{
Field: err.Field(),
Message: "",
Code: err.Tag(),
})
}
var res models.APIResponse
res.Message = "INVALID_PAYLOAD"
res.ErrorFields = fieldErr
res.StatusCode = http.StatusBadRequest
return c.JSON(http.StatusBadRequest, res)
}
var emailsAddress []string
for _, u := range payload.Users {
emailsAddress = append(emailsAddress, u.Email)
}
var calendarICSFileAttendees string
var calendarICSFileOrganizer string
// create ics file
if !reflect.ValueOf(payload.Invitation).IsNil() {
startAt, err := time.Parse(time.RFC3339, payload.Invitation.StartAt)
if err != nil {
log.Printf("Error parsing StartAt value: %s", payload.Invitation.StartAt)
return c.JSON(http.StatusInternalServerError, API500Error)
}
endAt, err := time.Parse(time.RFC3339, payload.Invitation.EndAt)
if err != nil {
log.Printf("Error parsing EndAt value: %s", payload.Invitation.EndAt)
return c.JSON(http.StatusInternalServerError, API500Error)
}
var attendees = make(map[string]string)
for _, u := range payload.Users {
attendees[u.Email] = u.FullName
}
// we need to create two separate ics files
// otherwise the organizator won't be able
// to add the event automatically to his calendar
// this is why we usee ics.MethodRequest for attendees
// and ics.MethodPublish for the organizer
// https://datatracker.ietf.org/doc/html/rfc2446#section-3.2
icsAttendees := createICS(startAt, endAt, payload.Invitation.EventSummary, payload.Invitation.Description, payload.Invitation.Location, payload.Invitation.OrganizerEmail, payload.Invitation.OrganizerFullName, attendees, ics.MethodRequest)
icsOrganizer := createICS(startAt, endAt, payload.Invitation.EventSummary, payload.Invitation.Description, payload.Invitation.Location, payload.Invitation.OrganizerEmail, payload.Invitation.OrganizerFullName, attendees, ics.MethodPublish)
icsFileAttendees, err := ioutil.TempFile(os.TempDir(), "*.ics")
if err != nil {
log.Printf("Failed to write to temporary file: %s", err)
return c.JSON(http.StatusInternalServerError, API500Error)
}
icsFileOrganizer, err := ioutil.TempFile(os.TempDir(), "*.ics")
if err != nil {
log.Printf("Failed to write to temporary file: %s", err)
return c.JSON(http.StatusInternalServerError, API500Error)
}
icsFileAttendees.Write([]byte(icsAttendees))
icsFileOrganizer.Write([]byte(icsOrganizer))
// Close the file
err = icsFileAttendees.Close()
if err != nil {
log.Printf("Unable to close file %s", err)
return c.JSON(http.StatusInternalServerError, API500Error)
}
err = icsFileOrganizer.Close()
if err != nil {
log.Printf("Unable to close file %s", err)
return c.JSON(http.StatusInternalServerError, API500Error)
}
calendarICSFileAttendees = icsFileAttendees.Name()
calendarICSFileOrganizer = icsFileOrganizer.Name()
defer os.Remove(icsFileAttendees.Name())
defer os.Remove(icsFileOrganizer.Name())
}
// send emails to users
if settings.SendUsing == "MAILGUN" {
_, err = sendEmailMailgun(payload.EmailSubject, payload.EmailBody, payload.EmailIsHTML, emailsAddress, calendarICSFileAttendees)
} else {
err = sendEmailSMTP(emailsAddress, payload.EmailSubject, payload.EmailBody, payload.EmailIsHTML, calendarICSFileAttendees)
}
if err != nil {
return c.JSON(http.StatusInternalServerError, API500Error)
}
// send a separate email to the organizer
// so the event gets created on his calendar
// (Only if an invitation is created)
if calendarICSFileOrganizer != "" {
var organizer []string
organizer = append(organizer, payload.Invitation.OrganizerEmail)
// send email to organizer
if settings.SendUsing == "MAILGUN" {
_, err = sendEmailMailgun(payload.EmailSubject, payload.EmailBody, payload.EmailIsHTML, organizer, calendarICSFileOrganizer)
} else {
err = sendEmailSMTP(organizer, payload.EmailSubject, payload.EmailBody, payload.EmailIsHTML, calendarICSFileOrganizer)
}
}
if err != nil {
return c.JSON(http.StatusInternalServerError, API500Error)
}
var res models.APIResponse
res = models.APIResponse{
Message: "SENT_OK",
StatusCode: http.StatusOK,
}
return c.JSON(res.StatusCode, res)
}
func sendEmailMailgun(subject string, body string, isHTML bool, recipients []string, attachment string) (string, error) {
mg := mailgun.NewMailgun(settings.Mailgun.Domain, settings.Mailgun.SecretKey)
sender := settings.SenderAddress
message := mg.NewMessage(sender, subject, body, recipients[0])
if isHTML {
message.SetHtml(body)
}
if attachment != "" {
message.AddAttachment(attachment)
}
for _, e := range recipients {
message.AddCC(e)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
// Send the message with a 10 second timeout
_, id, err := mg.Send(ctx, message)
if err != nil {
log.Println(err)
return "", err
}
return id, nil
}
func sendEmailSMTP(recipients []string, subject string, body string, isHTML bool, attachment string) error {
m := gomail.NewMessage()
m.SetHeader("From", settings.SenderAddress)
m.SetHeader("Subject", subject)
for _, email := range recipients {
m.SetHeader("To", email)
}
if isHTML {
m.SetBody("text/html", body)
} else {
m.SetBody("text/plain", body)
}
m.Attach(attachment)
// Settings for SMTP server
port, err := strconv.Atoi(settings.SMTP.Port)
if err != nil {
fmt.Println(err)
return err
}
d := gomail.NewDialer(settings.SMTP.Host, port, settings.SMTP.User, settings.SMTP.Password)
// Now send E-Mail
if err := d.DialAndSend(m); err != nil {
fmt.Println(err)
}
return err
}
func createICS(startAt time.Time, endAt time.Time, summary string, description string, location string, organizerEmail string, organizerFullName string, attendees map[string]string, methodRequest ics.Method) string {
cal := ics.NewCalendar()
cal.SetMethod(methodRequest)
event := cal.AddEvent(fmt.Sprintf("%s%s", uuid.New(), organizerEmail))
event.SetTimeTransparency(ics.TransparencyOpaque)
event.SetCreatedTime(time.Now())
event.SetDtStampTime(time.Now())
event.SetStartAt(startAt)
event.SetEndAt(endAt)
event.SetSummary(fmt.Sprintf("%s", summary))
event.SetDescription(fmt.Sprintf("%s", description))
event.SetLocation(fmt.Sprintf("%s", location))
event.SetOrganizer(organizerEmail, ics.WithCN(organizerFullName))
for email, fullName := range attendees {
event.AddAttendee(email, ics.WithCN(fullName), ics.CalendarUserTypeIndividual, ics.ParticipationStatusNeedsAction, ics.ParticipationRoleReqParticipant, ics.WithRSVP(true))
}
return cal.Serialize()
}
func main() {
// Echo instance
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
Level: 5,
}))
e.GET("/healthcheck", HealthcheckHandler)
e.POST("/invite/", InviteHandler)
// set default port
if settings.ServicePort == "" {
settings.ServicePort = "8000"
}
// Start server
go func() {
if err := e.Start(fmt.Sprintf(":%s", settings.ServicePort)); err != nil && err != http.ErrServerClosed {
e.Logger.Fatal("shutting down the server")
}
}()
// Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds.
// Use a buffered channel to avoid missing signals as recommended for signal.Notify
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := e.Shutdown(ctx); err != nil {
e.Logger.Fatal(err)
}
}