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

Unable to read requestContext data from event in fiber #158

Open
Shahab96 opened this issue Jan 13, 2023 · 2 comments
Open

Unable to read requestContext data from event in fiber #158

Shahab96 opened this issue Jan 13, 2023 · 2 comments

Comments

@Shahab96
Copy link

Shahab96 commented Jan 13, 2023

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
Copy link

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
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants