Skip to content

Commit 73df742

Browse files
committed
wip
1 parent 7ee1944 commit 73df742

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

aws/adaptor.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,20 @@ const (
1818
APIGatewayWebsocketIntegration
1919
APIGatewayHTTPIntegration
2020
ALBTargetGroupIntegration
21+
LambdaFunctionURLIntegration
2122
)
2223

2324
type integrationTypeChecker struct {
2425
// 'resource' parameter only has REST API event.
2526
Resource *string `json:"resource"`
26-
// 'version' parameter only has HTTP API mode event.
27+
// 'version' only has API Gateway V2 payload (HTTP API mode or Lambda FunctionURL).
2728
Version *string `json:"version"`
29+
// 'pathParameters' parameter only has API Gateway V2 payload.
30+
// However, it is always nil for Function URLs.
31+
PathParameters map[string]string `json:"pathParameters"`
32+
// 'routeKey' parameter only has API Gateway V2 payload.
33+
// However, in the case of Function URLs, it is always $default.
34+
RouteKey string `json:"routeKey"`
2835
// 'http_method' parameter has event of API Gateway REST API mode and ALB target group mode.
2936
// However, ALB target group mode has not 'resource' parameter.
3037
HTTPMethod *string `json:"httpMethod"`
@@ -47,6 +54,9 @@ func (t integrationTypeChecker) IntegrationType() LambdaIntegrationType {
4754
return APIGatewayWebsocketIntegration
4855
}
4956
if t.Version != nil {
57+
if t.RouteKey == "$default" && t.PathParameters == nil {
58+
return LambdaFunctionURLIntegration
59+
}
5060
return APIGatewayHTTPIntegration
5161
}
5262
if t.HTTPMethod != nil && t.Resource == nil {

aws/lambda.go

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ import (
77
"github.com/aws/aws-lambda-go/events"
88
"github.com/aws/aws-lambda-go/lambda"
99
"github.com/aws/aws-lambda-go/lambda/handlertrace"
10+
"github.com/aws/aws-lambda-go/lambdaurl"
1011
"github.com/aws/aws-sdk-go-v2/aws"
1112
"github.com/aws/aws-sdk-go-v2/config"
1213
"github.com/aws/aws-sdk-go/aws/session"
1314
"net/http"
15+
"os"
1416
)
1517

18+
var DEBUGDumpPayload = os.Getenv("DEBUG_DUMP_PAYLOAD")
19+
1620
const DefaultNonHTTPEventPath = "/events"
1721

1822
type LambdaHandlerOption func(handler *LambdaHandler)
@@ -179,6 +183,17 @@ func (l *LambdaHandler) InvokeWebsocketAPI(ctx context.Context, request *events.
179183
}
180184

181185
func (l *LambdaHandler) Invoke(ctx context.Context, payload []byte) ([]byte, error) {
186+
if DEBUGDumpPayload != "" && (DEBUGDumpPayload == "1" || DEBUGDumpPayload == "true") {
187+
ctx = handlertrace.NewContext(ctx, handlertrace.HandlerTrace{
188+
RequestEvent: func(ctx context.Context, payload interface{}) {
189+
fmt.Printf("Request payload: %s\n", payload)
190+
},
191+
ResponseEvent: func(ctx context.Context, payload interface{}) {
192+
fmt.Printf("Response payload: %s\n", payload)
193+
},
194+
})
195+
}
196+
182197
trace := handlertrace.FromContext(ctx)
183198

184199
var (
@@ -187,6 +202,10 @@ func (l *LambdaHandler) Invoke(ctx context.Context, payload []byte) ([]byte, err
187202
err error
188203
)
189204

205+
if trace.RequestEvent != nil {
206+
trace.RequestEvent(ctx, payload)
207+
}
208+
190209
if err = json.Unmarshal(payload, &checker); err != nil {
191210
res, err = l.HandleNonHTTPEvent(ctx, payload, http.DetectContentType(payload))
192211
} else {
@@ -196,34 +215,31 @@ func (l *LambdaHandler) Invoke(ctx context.Context, payload []byte) ([]byte, err
196215
if err := json.Unmarshal(payload, event); err != nil {
197216
return nil, err
198217
}
199-
if trace.RequestEvent != nil {
200-
trace.RequestEvent(ctx, payload)
201-
}
202218
res, err = l.InvokeRESTAPI(ctx, event)
203219
case APIGatewayHTTPIntegration:
204220
event := &events.APIGatewayV2HTTPRequest{}
205221
if err := json.Unmarshal(payload, event); err != nil {
206222
return nil, err
207223
}
208-
if trace.RequestEvent != nil {
209-
trace.RequestEvent(ctx, payload)
210-
}
211224
res, err = l.InvokeHTTPAPI(ctx, event)
212225
case ALBTargetGroupIntegration:
213226
event := &events.ALBTargetGroupRequest{}
214227
if err := json.Unmarshal(payload, event); err != nil {
215228
return nil, err
216229
}
217-
if trace.RequestEvent != nil {
218-
trace.RequestEvent(ctx, payload)
219-
}
220230
res, err = l.InvokeALBTargetGroup(ctx, event)
221231
case APIGatewayWebsocketIntegration:
222232
event := &events.APIGatewayWebsocketProxyRequest{}
223233
if err := json.Unmarshal(payload, event); err != nil {
224234
return nil, err
225235
}
226236
res, err = l.InvokeWebsocketAPI(ctx, event)
237+
case LambdaFunctionURLIntegration:
238+
event := &events.LambdaFunctionURLRequest{}
239+
if err := json.Unmarshal(payload, event); err != nil {
240+
return nil, err
241+
}
242+
res, err = lambdaurl.Wrap(l.httpHandler)(ctx, event)
227243
default:
228244
res, err = l.HandleNonHTTPEvent(ctx, payload, "application/json")
229245
}
@@ -233,16 +249,18 @@ func (l *LambdaHandler) Invoke(ctx context.Context, payload []byte) ([]byte, err
233249
return nil, err
234250
}
235251

236-
if trace.ResponseEvent != nil {
237-
trace.ResponseEvent(ctx, res)
238-
}
239-
240252
if b, ok := res.([]byte); ok {
253+
if trace.ResponseEvent != nil {
254+
trace.ResponseEvent(ctx, b)
255+
}
241256
return b, nil
242257
} else {
243258
if responseBytes, err := json.Marshal(res); err != nil {
244259
return nil, err
245260
} else {
261+
if trace.ResponseEvent != nil {
262+
trace.ResponseEvent(ctx, responseBytes)
263+
}
246264
return responseBytes, nil
247265
}
248266
}

0 commit comments

Comments
 (0)