Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support SQSEvent #40

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const (
RequestTypeAPIGatewayV1
RequestTypeAPIGatewayV2
RequestTypeALB
RequestTypeSQS
)

// Options holds the optional parameters.
Expand Down
13 changes: 11 additions & 2 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}

Expand Down
2 changes: 2 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions sqs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package algnhsa

import (
"context"
"encoding/json"
"errors"
"net/http"

"github.com/aws/aws-lambda-go/events"
)

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
}