/
main.go
77 lines (65 loc) · 1.88 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
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/joshuous/haveibeenbreached"
)
type Response events.APIGatewayProxyResponse
/*
TODO:
- validate email
- add email to SQS
*/
type NotifyMeEvent struct {
Email string
}
var sess = session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
var db = dynamodb.New(sess)
var r = haveibeenbreached.NewRepo(db)
var sqsQueue = sqs.New(sess)
var mq = haveibeenbreached.NewMessageQueue(sqsQueue)
var notifyMeHandler = makeNotifyMeHandler(r, mq)
var subscriptionsQueueURL = os.Getenv("SUBSCRIPTIONS_QUEUE_URL")
func main() {
lambda.Start(notifyMeHandler)
}
func makeNotifyMeHandler(repo haveibeenbreached.Repo, queue haveibeenbreached.MessageQueue) func(ctx context.Context, event NotifyMeEvent) (Response, error) {
return func(ctx context.Context, event NotifyMeEvent) (Response, error) {
rawEmail := event.Email
subscriber, err := haveibeenbreached.NewSubscriber(rawEmail)
if err != nil {
return Response{StatusCode: 400, Body: fmt.Sprintf("Invalid email: %s", rawEmail)}, err
}
err = queue.SendMessage(haveibeenbreached.SendMessageInput{
MessageBody: subscriber.Email,
QueueURL: subscriptionsQueueURL,
})
if err != nil {
return Response{StatusCode: 400}, err
}
body, err := json.Marshal(map[string]interface{}{
"message": "Processing breach notification subscription.",
})
if err != nil {
return Response{StatusCode: 400}, err
}
resp := Response{
StatusCode: 202,
IsBase64Encoded: false,
Body: string(body),
Headers: map[string]string{
"Content-Type": "application/json",
},
}
return resp, nil
}
}