@@ -9,20 +9,17 @@ import (
9
9
"net/http"
10
10
"net/url"
11
11
"strings"
12
+ "github.com/aws/aws-lambda-go/events"
12
13
)
13
14
14
15
func NewALBPayloadBuilder (enableMultiValue bool ) PayloadBuilder {
15
- if enableMultiValue {
16
- return ALBMultiValuePayloadBuilder {}
17
- } else {
18
- return ALBPayloadBuilder {}
16
+ return ALBPayloadBuilder {
17
+ EnableMultiValue : enableMultiValue ,
19
18
}
20
19
}
21
20
22
21
type ALBPayloadBuilder struct {
23
- }
24
-
25
- type ALBMultiValuePayloadBuilder struct {
22
+ EnableMultiValue bool
26
23
}
27
24
28
25
func (pb ALBPayloadBuilder ) BuildRequest (r * http.Request ) ([]byte , error ) {
@@ -36,98 +33,51 @@ func (pb ALBPayloadBuilder) BuildRequest(r *http.Request) ([]byte, error) {
36
33
return nil , err
37
34
}
38
35
39
- event := struct {
40
- HttpMethod string `json:"httpMethod"`
41
- Path string `json:"path"`
42
- QueryStringParameters map [string ]string `json:"queryStringParameters"`
43
- Headers map [string ]string `json:"headers"`
44
- Body string `json:"body"`
45
- IsBase64Encoded bool `json:"isBase64Encoded"`
46
- }{
47
- HttpMethod : r .Method ,
36
+ event := events.ALBTargetGroupRequest {
37
+ HTTPMethod : r .Method ,
48
38
Path : r .URL .Path ,
49
- QueryStringParameters : ArrayMapToFirstElementMap (queryParams ),
50
- Headers : ArrayMapToFirstElementMap (r .Header ),
51
39
Body : body ,
52
40
IsBase64Encoded : isBase64Encoded ,
41
+ RequestContext : events.ALBTargetGroupRequestContext {
42
+ ELB : events.ELBContext {
43
+ TargetGroupArn : "arn:aws:elasticloadbalancing:us-east-2:123456789012:targetgroup/lambda-local-proxy-dummy/1234567890123456" ,
44
+ },
45
+ },
53
46
}
54
47
55
- return json .Marshal (event )
56
- }
57
-
58
- func (pb ALBMultiValuePayloadBuilder ) BuildRequest (r * http.Request ) ([]byte , error ) {
59
- queryParams , err := url .ParseQuery (r .URL .RawQuery )
60
- if err != nil {
61
- return nil , err
62
- }
63
-
64
- body , isBase64Encoded , err := ReadBodyAsString (r )
65
- if err != nil {
66
- return nil , err
67
- }
68
-
69
- event := struct {
70
- HttpMethod string `json:"httpMethod"`
71
- Path string `json:"path"`
72
- MultiValueQueryStringParameters map [string ][]string `json:"multiValueQueryStringParameters"`
73
- MultiValueHeaders map [string ][]string `json:"multiValueHeaders"`
74
- Body string `json:"body"`
75
- IsBase64Encoded bool `json:"isBase64Encoded"`
76
- }{
77
- HttpMethod : r .Method ,
78
- Path : r .URL .Path ,
79
- MultiValueQueryStringParameters : queryParams ,
80
- MultiValueHeaders : r .Header ,
81
- Body : body ,
82
- IsBase64Encoded : isBase64Encoded ,
48
+ if pb .EnableMultiValue {
49
+ event .MultiValueQueryStringParameters = queryParams
50
+ event .MultiValueHeaders = r .Header
51
+ } else {
52
+ event .QueryStringParameters = ArrayMapToFirstElementMap (queryParams )
53
+ event .Headers = ArrayMapToFirstElementMap (r .Header )
83
54
}
84
55
85
56
return json .Marshal (event )
86
57
}
87
58
88
59
func (pb ALBPayloadBuilder ) BuildResponse (blob []byte ) (int , []byte , map [string ][]string , error ) {
89
- resp := struct {
90
- StatusCode int `json:"statusCode"`
91
- Headers map [string ]string `json:"headers"`
92
- Body string `json:"body"`
93
- IsBase64Encoded bool `json:"isBase64Encoded"`
94
- }{}
60
+ resp := events.ALBTargetGroupResponse {}
95
61
96
62
err := json .Unmarshal (blob , & resp )
97
63
64
+ var body []byte
98
65
if err != nil {
99
66
return BuildErrorResponse (err , "Invalid JSON response" )
100
67
} else if resp .IsBase64Encoded {
101
68
binary , err := base64 .StdEncoding .DecodeString (resp .Body )
102
69
if err != nil {
103
70
return BuildErrorResponse (err , "Invalid body in JSON response" )
104
71
}
105
- return resp . StatusCode , binary , MapToArrayMap ( resp . Headers ), nil
72
+ body = binary
106
73
} else {
107
- return resp . StatusCode , []byte (resp .Body ), MapToArrayMap ( resp . Headers ), nil
74
+ body = []byte (resp .Body )
108
75
}
109
- }
110
-
111
- func (pb ALBMultiValuePayloadBuilder ) BuildResponse (blob []byte ) (int , []byte , map [string ][]string , error ) {
112
- resp := struct {
113
- StatusCode int `json:"statusCode"`
114
- MultiValueHeaders map [string ][]string `json:"multiValueHeaders"`
115
- Body string `json:"body"`
116
- IsBase64Encoded bool `json:"isBase64Encoded"`
117
- }{}
118
-
119
- err := json .Unmarshal (blob , & resp )
120
76
121
- if err != nil {
122
- return BuildErrorResponse (err , "Invalid JSON response" )
123
- } else if resp .IsBase64Encoded {
124
- binary , err := base64 .StdEncoding .DecodeString (resp .Body )
125
- if err != nil {
126
- return BuildErrorResponse (err , "Invalid body in JSON response" )
127
- }
128
- return resp .StatusCode , binary , resp .MultiValueHeaders , nil
77
+ if pb .EnableMultiValue {
78
+ return resp .StatusCode , body , resp .MultiValueHeaders , nil
129
79
} else {
130
- return resp .StatusCode , [] byte ( resp . Body ), resp .MultiValueHeaders , nil
80
+ return resp .StatusCode , body , MapToArrayMap ( resp .Headers ) , nil
131
81
}
132
82
}
133
83
0 commit comments