-
Notifications
You must be signed in to change notification settings - Fork 0
/
kinesis_event.go
57 lines (45 loc) · 1.4 KB
/
kinesis_event.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
package lambdabase
import (
"context"
"encoding/json"
"fmt"
"github.com/aws/aws-lambda-go/events"
"github.com/go-nacelle/nacelle/v2"
)
type (
KinesisEventHandler interface {
Handle(ctx context.Context, batch []events.KinesisEventRecord, logger nacelle.Logger) error
}
kinesisEventHandlerInitializer interface {
nacelle.Initializer
KinesisEventHandler
}
kinesisEventHandler struct {
Logger nacelle.Logger `service:"logger"`
Services *nacelle.ServiceContainer `service:"services"`
handler KinesisEventHandler
}
)
func NewKinesisEventServer(handler KinesisEventHandler) *Server {
return NewServer(&kinesisEventHandler{
handler: handler,
})
}
func (h *kinesisEventHandler) Init(ctx context.Context) error {
return doInit(ctx, h.Services, h.handler)
}
func (h *kinesisEventHandler) Invoke(ctx context.Context, payload []byte) ([]byte, error) {
event := &events.KinesisEvent{}
if err := json.Unmarshal(payload, &event); err != nil {
return nil, fmt.Errorf("failed to unmarshal event (%s)", err.Error())
}
logger := h.Logger.WithFields(map[string]interface{}{
"requestId": GetRequestID(ctx),
})
logger.Debug("Received %d Kinesis records", len(event.Records))
if err := h.handler.Handle(ctx, event.Records, logger); err != nil {
return nil, fmt.Errorf("failed to process Kinesis event (%s)", err.Error())
}
logger.Debug("Kinesis event handled successfully")
return nil, nil
}