awslabs / aws-lambda-go-api-proxy

lambda-go-api-proxy makes it easy to port APIs written with Go frameworks such as Gin (https://gin-gonic.github.io/gin/ ) to AWS Lambda and Amazon API Gateway.
Apache License 2.0
1.09k stars 200 forks source link

Unable to read requestContext data from event in fiber #158

Open Shahab96 opened 1 year ago

Shahab96 commented 1 year ago

I'm currently using fiber for my app and have a handler which will be given data passed through by a custom auth lambda. I am unable to access the lambda event from within my handler however. I'm currently using ProxyWithContextV2 to run my app, but I can't seem to find a way to extract the event data from within the handler.

func init() {
    log.Printf("Cold start.")

    app = fiber.New()

    app.Post("/auth/login", routes.Login)
    app.Post("/auth/register", routes.Register)
    app.Post("/auth/logout", routes.Logout)
    fiberLambda = fiberadapter.New(app)
}

func handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
    return fiberLambda.ProxyWithContextV2(ctx, req)
}

func main() {
    if os.Getenv("AWS_EXECUTION_ENV") == "AWS_Lambda_go1.x" {
        lambda.Start(handler)
    } else {
        app.Listen(":3000")
    }
}
func Logout(c *fiber.Ctx) error {
  // Need to get the event.requestContext.authorizer here
}
kasperbe commented 1 year ago

I am currently running in to the same problem using the negroni adapter.

I have come to the conclusion that there's a problem with ProxyWithContext. It seems the requestContext is not added to the request.

If you're willing to give up the request context you can use fiberLambda.Proxy, however this is not a real solution.

To bypass this for now we can add the requestContext to the context manully:

    return fiberLambda.ProxyWithContext(context.WithValue(ctx, requestContext, req.RequestContext), req)

And in your handler, access the requestContext using:

    reqCtx := req.Context().Value(RequestContext)

@sapessi (Sorry for the direct tag) is this inteded, and if not, I could provide a fix to add the requestContext on the ProxyWithContext. What do you think? :)

esoteloferry commented 1 year ago

Hi @Shahab96 , I am using same setup Fiber+HTTP(v2) gateway + aws-lambda-go-api-proxy v0.14.0.

I implemented a Post /auth/login endpoint as dummy endpoing and I am struggling with next error

{
  "statusCode": 404,
  "headers": {
    "Content-Type": "text/plain; charset=utf-8"
  },
  "multiValueHeaders": null,
  "body": "Cannot GET /",
  "cookies": []
}

All requests are pointing to GET / , I wonder if you had this issue? or you can share more details of your setup?