-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (42 loc) · 1.09 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
package main
import (
"context"
"encoding/json"
"mailer/go-lambda/services"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
type RequestBody struct {
Email string `json:"email"`
EmailType string `json:"emailType"`
}
func handler(context context.Context, event events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
var emailBody RequestBody
err := json.Unmarshal([]byte(event.Body), &emailBody)
if err != nil {
return events.APIGatewayProxyResponse{
StatusCode: 422,
Body: "Invalid JSON payload",
}, nil
}
if emailBody.Email == "" || emailBody.EmailType == "" {
return events.APIGatewayProxyResponse{
StatusCode: 422,
Body: "email or emailType is blank or null",
}, nil
}
err = services.SaveEmail(emailBody.Email, emailBody.EmailType)
if err != nil {
return events.APIGatewayProxyResponse{
StatusCode: 422,
Body: "got error while saving the email",
}, nil
}
return events.APIGatewayProxyResponse{
StatusCode: 201,
Body: "email sent",
}, nil
}
func main() {
lambda.Start(handler)
}