From 5a3e26187007999b11b2125edcbccb61e245c885 Mon Sep 17 00:00:00 2001 From: Benjamin Schulz Date: Thu, 29 Aug 2024 20:18:25 +0200 Subject: [PATCH 1/2] Support SQSEvent --- options.go | 1 + request.go | 13 +++++++++++-- response.go | 2 ++ sqs.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 sqs.go diff --git a/options.go b/options.go index 0da3ec4..2297e4a 100644 --- a/options.go +++ b/options.go @@ -7,6 +7,7 @@ const ( RequestTypeAPIGatewayV1 RequestTypeAPIGatewayV2 RequestTypeALB + RequestTypeSQS ) // Options holds the optional parameters. diff --git a/request.go b/request.go index 89e94dc..a2bb253 100644 --- a/request.go +++ b/request.go @@ -10,7 +10,7 @@ import ( "strings" ) -var errUnsupportedPayloadFormat = errors.New("unsupported payload format; supported formats: APIGatewayV2HTTPRequest, APIGatewayProxyRequest, ALBTargetGroupRequest") +var errUnsupportedPayloadFormat = errors.New("unsupported payload format; supported formats: APIGatewayV2HTTPRequest, APIGatewayProxyRequest, ALBTargetGroupRequest, SQSEvent") type lambdaRequest struct { HTTPMethod string @@ -35,10 +35,12 @@ func newLambdaRequest(ctx context.Context, payload []byte, opts *Options) (lambd return newAPIGatewayV2Request(ctx, payload, opts) case RequestTypeALB: return newALBRequest(ctx, payload, opts) + case RequestTypeSQS: + return newSQSRequest(ctx, payload, opts) } // The request type wasn't specified. - // Try to decode the payload as APIGatewayV2HTTPRequest, fall back to APIGatewayProxyRequest, then ALBTargetGroupRequest. + // Try to decode the payload as APIGatewayV2HTTPRequest, fall back to APIGatewayProxyRequest, then ALBTargetGroupRequest, then SQSRequest. req, err := newAPIGatewayV2Request(ctx, payload, opts) if err != nil && err != errAPIGatewayV2UnexpectedRequest { return lambdaRequest{}, err @@ -63,6 +65,13 @@ func newLambdaRequest(ctx context.Context, payload []byte, opts *Options) (lambd return req, nil } + req, err = newSQSRequest(ctx, payload, opts) + if err != nil && err != errSQSUnexpectedRequest { + return lambdaRequest{}, err + } + if err == nil { + return req, nil + } return lambdaRequest{}, errUnsupportedPayloadFormat } diff --git a/response.go b/response.go index e7b8500..5ae9ad1 100644 --- a/response.go +++ b/response.go @@ -33,6 +33,8 @@ func newLambdaResponse(w *httptest.ResponseRecorder, binaryContentTypes map[stri resp, err = newALBResponse(result) case RequestTypeAPIGatewayV2: resp, err = newAPIGatewayV2Response(result) + case RequestTypeSQS: + resp, err = newSQSResponse(result) } if err != nil { return resp, err diff --git a/sqs.go b/sqs.go new file mode 100644 index 0000000..47fc213 --- /dev/null +++ b/sqs.go @@ -0,0 +1,49 @@ +package algnhsa + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "net/http" + + "github.com/aws/aws-lambda-go/events" +) + +// RequestTypeSQS +// https://github.com/aws/aws-lambda-go/blob/v1.47.0/events/sqs.go + +var ( + errSQSUnexpectedRequest = errors.New("expected SQSRequest event") +) + +func newSQSRequest(ctx context.Context, payload []byte, opts *Options) (lambdaRequest, error) { + var event events.SQSEvent + if err := json.Unmarshal(payload, &event); err != nil { + return lambdaRequest{}, err + } + body, err := json.Marshal(event.Records) + req := lambdaRequest{ + HTTPMethod: "POST", + Path: "/sqs", + Body: string(body), + Context: context.WithValue(ctx, RequestTypeSQS, event), + requestType: RequestTypeSQS, + } + return req, err +} + +func newSQSResponse(r *http.Response) (lambdaResponse, error) { + resp := lambdaResponse{} + return resp, nil +} + +// SQSRequestFromContext extracts the SQSEvent from ctx. +func SQSRequestFromContext(ctx context.Context) (events.SQSEvent, bool) { + val := ctx.Value(RequestTypeSQS) + if val == nil { + return events.SQSEvent{}, false + } + event, ok := val.(events.SQSEvent) + return event, ok +} From bbd208e5ef0b0afa4e266526f40f65778badd03a Mon Sep 17 00:00:00 2001 From: Benjamin Schulz Date: Fri, 30 Aug 2024 10:14:20 +0200 Subject: [PATCH 2/2] Remove unused fmt and deprecated comment --- sqs.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/sqs.go b/sqs.go index 47fc213..2778d30 100644 --- a/sqs.go +++ b/sqs.go @@ -4,15 +4,11 @@ import ( "context" "encoding/json" "errors" - "fmt" "net/http" "github.com/aws/aws-lambda-go/events" ) -// RequestTypeSQS -// https://github.com/aws/aws-lambda-go/blob/v1.47.0/events/sqs.go - var ( errSQSUnexpectedRequest = errors.New("expected SQSRequest event") )