-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
80 lines (63 loc) · 1.86 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"
"emailAvailability/core"
"encoding/json"
"errors"
"github.com/gofiber/fiber/v2"
"github.com/joho/godotenv"
"log"
"os"
)
func main() {
errEnv := godotenv.Load()
if errEnv != nil {
log.Fatal("Error loading .env file")
}
app := fiber.New()
app.Get("/webhooks", func(ctx *fiber.Ctx) error {
if challenge := ctx.Query("challenge"); challenge != "" {
return ctx.Status(fiber.StatusOK).SendString(challenge)
}
return ctx.SendStatus(fiber.StatusBadRequest)
})
app.Post("/webhooks", func(ctx *fiber.Ctx) error {
ctx.Accepts("application/json")
signature := ctx.GetReqHeaders()["X-Nylas-Signature"]
if err := core.CheckSignature(os.Getenv("NYLAS_CLIENT_SECRET"), signature, ctx.Body()); err != nil {
return ctx.SendStatus(fiber.StatusUnauthorized)
}
w := new(core.WebhookRequest)
if e := json.Unmarshal(ctx.Body(), w); e != nil {
log.Println("Error parsing webhook body into WebhookRequest", e)
return ctx.SendStatus(fiber.StatusBadRequest)
}
w.LogInfo()
return ctx.SendStatus(fiber.StatusOK)
})
app.Post("/send", func(ctx *fiber.Ctx) error {
ctx.Accepts("application/json")
sendRes, injectErr := core.InjectAvailabilityAndSendMessage(ctx.Body(), ctx.GetReqHeaders())
if injectErr != nil {
res := core.MessageResponse{
Success: false,
ErrorMessage: injectErr.Error(),
}
if reqErr, ok := injectErr.(*core.RequestError); ok && reqErr.StatusCode != 0 {
ctx.Status(reqErr.StatusCode)
} else if errors.Is(reqErr, context.DeadlineExceeded) {
res.ErrorMessage = "API timeout calling Nylas"
} else {
ctx.Status(fiber.StatusInternalServerError)
}
b, _ := json.Marshal(res)
return ctx.Send(b)
}
b, _ := json.Marshal(core.MessageResponse{
Success: true,
Data: sendRes,
})
return ctx.Status(fiber.StatusOK).Send(b)
})
log.Fatal(app.Listen(":8000"))
}