-
Notifications
You must be signed in to change notification settings - Fork 32
/
ses.go
77 lines (66 loc) · 2.56 KB
/
ses.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 ses provides structs for working with AWS SES records.
package ses
import (
"encoding/json"
"time"
"github.com/apex/go-apex"
)
// Event represents a SES event with one or more records.
type Event struct {
Records []*Record `json:"Records"`
}
// Record represents a single SES record.
type Record struct {
EventSource string `json:"eventSource"`
EventVersion string `json:"eventVersion"`
SES struct {
Receipt struct {
Type string `json:"Type"`
MessageID string `json:"MessageID"`
TopicARN string `json:"TopicArn"`
Subject string `json:"Subject"`
Message []byte `json:"Message"`
Timestamp time.Time `json:"Timestamp"`
SignatureVersion string `json:"SignatureVersion"`
Signature string `json:"Signature"`
SignatureCertURL string `json:"SignatureCertURL"`
UnsubscribeURL string `json:"UnsubscribeURL"`
MessageAttributes map[string]interface{} `json:"MessageAttributes"`
} `json:"receipt"`
Mail struct {
Type string `json:"Type"`
MessageID string `json:"MessageID"`
TopicARN string `json:"TopicArn"`
Subject string `json:"Subject"`
Message []byte `json:"Message"`
Timestamp time.Time `json:"Timestamp"`
SignatureVersion string `json:"SignatureVersion"`
Signature string `json:"Signature"`
SignatureCertURL string `json:"SignatureCertURL"`
UnsubscribeURL string `json:"UnsubscribeURL"`
MessageAttributes map[string]interface{} `json:"MessageAttributes"`
} `json:"mail"`
} `json:"ses"`
}
// Handler handles SES events.
type Handler interface {
HandleSES(*Event, *apex.Context) error
}
// HandlerFunc unmarshals SES events before passing control.
type HandlerFunc func(*Event, *apex.Context) error
// Handle implements apex.Handler.
func (h HandlerFunc) Handle(data json.RawMessage, ctx *apex.Context) (interface{}, error) {
var event Event
if err := json.Unmarshal(data, &event); err != nil {
return nil, err
}
return nil, h(&event, ctx)
}
// HandleFunc handles SES events with callback function.
func HandleFunc(h HandlerFunc) {
apex.Handle(h)
}
// Handle SES events with handler.
func Handle(h Handler) {
HandleFunc(HandlerFunc(h.HandleSES))
}