英文:
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())
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论