-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformer_alb.go
70 lines (60 loc) · 1.6 KB
/
transformer_alb.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
package delta
import (
"context"
"io"
"net/http"
"net/url"
"strings"
"github.com/aws/aws-lambda-go/events"
json "github.com/json-iterator/go"
)
type AlbTransformer struct{}
func WithALB() Options {
return WithTransformer(AlbTransformer{})
}
// Response implements Transformer
func (AlbTransformer) Response(_ context.Context, r *ResponseWriter) ([]byte, error) {
res := &events.ALBTargetGroupResponse{
StatusCode: r.status,
MultiValueHeaders: r.header,
IsBase64Encoded: r.encode,
Body: r.bodyString(),
}
return json.Marshal(res)
}
// Request implements Transformer
func (AlbTransformer) Request(ctx context.Context, payload []byte) (*http.Request, error) {
var e events.ALBTargetGroupRequest
json.Unmarshal(payload, &e)
header := convertToHttpHeader(e.Headers)
host := header.Get("host")
qs := url.Values(e.MultiValueQueryStringParameters)
u := url.URL{
Path: e.Path,
Host: host,
RawQuery: qs.Encode(),
Scheme: "https",
}
req := http.Request{
RequestURI: u.RequestURI(),
Method: e.HTTPMethod,
URL: &u,
// just hardcode it
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
// content
Header: header,
Body: io.NopCloser(strings.NewReader(e.Body)),
// from header
ContentLength: int64(len(e.Body)),
TransferEncoding: []string{},
Close: true,
Host: host,
RemoteAddr: header.Get("x-forwarded-for"),
}
return withContextEvent(&req, ctx, &e), nil
}
func GetAlbEvent(ctx context.Context) (*events.ALBTargetGroupRequest, error) {
return getEvent[*events.ALBTargetGroupRequest](ctx)
}