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.06k stars 198 forks source link

Support V2 payloads for Echo #104

Closed AlexLast closed 2 years ago

AlexLast commented 3 years ago

Issue #, if available:

Description of changes: Adds support for API Gateway HTTP V2 payloads for the Echo framework

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

yasuoza commented 2 years ago

Any update for this PR? This works fine for me.

AlexLast commented 2 years ago

Any update for this PR? This works fine for me.

@yasuoza Unfortunately It's still waiting for a review, currently I'm just using my own build

yasuoza commented 2 years ago

FYI: Bypass to echo.ServeHTTP also works. This may be useful until this PR is merged.

package main

import (
    "context"
    "log"
    "net/http"

    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"
    "github.com/awslabs/aws-lambda-go-api-proxy/handlerfunc"
    "github.com/labstack/echo/v4"
)

var (
    httpLambda *handlerfunc.HandlerFuncAdapterV2
)

func init() {
    // stdout and stderr are sent to AWS CloudWatch Logs
    log.Printf("echo cold start")
    e := echo.New()
    e.GET("/", func(c echo.Context) error {
        return c.JSON(http.StatusOK, map[string]string{"message": "pong"})
    })

    httpLambda = handlerfunc.NewV2(e.ServeHTTP)
}

// Handler handles request from API Gateway V2
func Handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
    return httpLambda.ProxyWithContext(ctx, req)
}

func main() {
    lambda.Start(Handler)
}