/
dispatcher.go
148 lines (136 loc) · 4.08 KB
/
dispatcher.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package toyhose
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/firehose"
"github.com/google/uuid"
)
// DispatcherConfig represents configuration data struct for Dispatcher.
type DispatcherConfig struct {
S3InjectedConf S3InjectedConf
KinesisInjectedConf KinesisInjectedConf
AWSConf *aws.Config
}
// S3InjectedConf represents injection to S3 destination BufferingHints forcely.
type S3InjectedConf struct {
SizeInMBs *int
IntervalInSeconds *int
EndPoint *string
DisableBuffering bool
}
// KinesisInjectedConf represents configuration of KinesisStream source.
type KinesisInjectedConf struct {
Endpoint *string
}
// NewDispatcher returns Dispatcher object.
func NewDispatcher(conf *DispatcherConfig) *Dispatcher {
cred, _ := conf.AWSConf.Credentials.Get()
return &Dispatcher{
conf: conf.AWSConf,
accountID: cred.AccessKeyID,
region: *conf.AWSConf.Region,
s3InjectedConf: conf.S3InjectedConf,
kinesisInjectedConf: conf.KinesisInjectedConf,
pool: &deliveryStreamPool{
pool: map[string]*deliveryStream{},
},
}
}
// Dispatcher represents firehose API handler.
type Dispatcher struct {
conf *aws.Config
accountID string
region string
s3InjectedConf S3InjectedConf
kinesisInjectedConf KinesisInjectedConf
pool *deliveryStreamPool
}
// Dispatch handlers HTTP request as http.HandlerFunc interface.
func (d *Dispatcher) Dispatch(w http.ResponseWriter, r *http.Request) {
reqID := uuid.New().String()
w.Header().Add("x-amzn-RequestId", reqID)
w.Header().Add("Content-Type", r.Header.Get("Content-Type"))
op, err := parseTarget(r.Header.Get("X-Amz-Target"))
if err != nil {
outputForJSON(w, nil, err)
return
}
b := bytes.NewBuffer([]byte{})
io.Copy(b, r.Body)
if err := verifyV4(d.conf, r, bytes.NewReader(b.Bytes())); err != nil {
outputForJSON(w, nil, err)
return
}
svc := &DeliveryStreamService{
awsConf: d.conf,
region: d.region,
accountID: d.accountID,
s3InjectedConf: d.s3InjectedConf,
kinesisInjectedConf: d.kinesisInjectedConf,
pool: d.pool,
}
switch op {
case "CreateDeliveryStream":
out, err := svc.Create(r.Context(), b.Bytes())
outputForJSON(w, out, err)
case "DeleteDeliveryStream":
out, err := svc.Delete(r.Context(), b.Bytes())
outputForJSON(w, out, err)
case "PutRecord":
out, err := svc.Put(r.Context(), b.Bytes())
outputForJSON(w, out, err)
case "PutRecordBatch":
out, err := svc.PutBatch(r.Context(), b.Bytes())
outputForJSON(w, out, err)
case "ListDeliveryStreams":
out, err := svc.Listing(r.Context(), b.Bytes())
outputForJSON(w, out, err)
case "DescribeDeliveryStream":
out, err := svc.Describe(r.Context(), b.Bytes())
outputForJSON(w, out, err)
default:
err := awserr.New("InvalidAction", "invalid action received", errInvalidTargetHeader)
outputForJSON(w, nil, err)
}
}
type outputSerializable interface {
String() string
GoString() string
}
func outputForJSON(w http.ResponseWriter, out outputSerializable, err error) {
if err == nil {
json.NewEncoder(w).Encode(out)
return
}
switch e := err.(type) {
case awserr.Error:
switch e.Code() {
case "InternalFailure":
w.WriteHeader(http.StatusInternalServerError)
case firehose.ErrCodeServiceUnavailableException:
w.WriteHeader(http.StatusServiceUnavailable)
case firehose.ErrCodeResourceNotFoundException:
w.WriteHeader(http.StatusNotFound)
default:
w.WriteHeader(http.StatusBadRequest)
}
default:
w.WriteHeader(http.StatusBadRequest)
}
json.NewEncoder(w).Encode(err)
}
var errInvalidTargetHeader = errors.New("invalid X-Amz-Target")
func parseTarget(tgt string) (string, error) {
// ex) Firehose_20150804.CreateDeliveryStream
parts := strings.Split(tgt, ".")
if len(parts) != 2 || !strings.HasPrefix(parts[0], "Firehose_") {
return "", awserr.New("MissingAction", "no action received", errInvalidTargetHeader)
}
return parts[1], nil
}