从gin.context中读取APIGatewayProxyRequest。

huangapple go评论73阅读模式
英文:

Read APIGatewayProxyRequest from gin.context

问题

我正在使用github.com/awslabs/aws-lambda-go-api-proxy/gin来创建一个API。
如果可能的话,我想从gin.Context中读取APIGatewayProxyRequest,或者请提供其他更好的方法?
这是我的请求处理程序:

func handleRequest(ctx context.Context, req *events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
	db := persistence.InitializeDB()
	defer db.Close()

	router := router.NewApiRouter()

	if ginLambda == nil {
		ginLambda = gin.New(router.Initialize()) // 在这里我将req发送到router,我想在端点处理程序中使用它
	}

	response, err := ginLambda.ProxyWithContext(ctx, *req)

   ...

	return response, err
}

这是我的post请求处理程序:

func (ser *Service) GetUserRecordByID(context *gin.Context) {
// 在这里我想读取APIGatewayProxyRequest对象。我可以记录context,但不确定如何读取APIGatewayProxyRequest对象?
}
英文:

I am using github.com/awslabs/aws-lambda-go-api-proxy/gin to create a API.
I want to read APIGatewayProxyRequest out of gin.Context if possible or please suggest other better way?
Here is my request handler,

func handleRequest(ctx context.Context, req *events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
	db := persistence.InitializeDB()
	defer db.Close()

	router := router.NewApiRouter()

	if ginLambda == nil {
		ginLambda = gin.New(router.Initialize()) // here I send req to router, which I want to use in endpoint handler
	}

	response, err := ginLambda.ProxyWithContext(ctx, *req)

   ...

	return response, err
}

Here is my handler for post request,

func (ser *Service) GetUserRecordByID(context *gin.Context) {
// here I want to read APIGatewayProxyRequest object. I can log context, but not sure how to read APIGatewayProxyRequest object? 
}

答案1

得分: 1

我已经找到了解决方案,使用aws lambda proxy core..

import (
"github.com/awslabs/aws-lambda-go-api-proxy/core"
)

func (ser *Service) GetUserRecordByID(context *gin.Context) {
apiGWRequestContext, ok := core.GetAPIGatewayContextFromContext(c.Request.Context())
}
英文:

I have found the solution, using aws lambda proxy core..

import (
"github.com/awslabs/aws-lambda-go-api-proxy/core"
)

func (ser *Service) GetUserRecordByID(context *gin.Context) {
apiGWRequestContext, ok := core.GetAPIGatewayContextFromContext(c.Request.Context())
}

huangapple
  • 本文由 发表于 2022年5月10日 22:41:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/72188407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定