Skip to content
This repository was archived by the owner on May 21, 2025. It is now read-only.

Commit 5099e0c

Browse files
committed
Support V2 payloads for Echo
1 parent 686c88b commit 5099e0c

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

echo/adapter.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
// creates a proxy response object from the http.ResponseWriter
1818
type EchoLambda struct {
1919
core.RequestAccessor
20+
v2 core.RequestAccessorV2
2021

2122
Echo *echo.Echo
2223
}
@@ -36,6 +37,12 @@ func (e *EchoLambda) Proxy(req events.APIGatewayProxyRequest) (events.APIGateway
3637
return e.proxyInternal(echoRequest, err)
3738
}
3839

40+
// ProxyV2 is the same as Proxy(), but for V2 payloads instead
41+
func (e *EchoLambda) ProxyV2(req events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
42+
echoRequest, err := e.v2.ProxyEventToHTTPRequest(req)
43+
return e.proxyInternalV2(echoRequest, err)
44+
}
45+
3946
// ProxyWithContext receives context and an API Gateway proxy event,
4047
// transforms them into an http.Request object, and sends it to the echo.Echo for routing.
4148
// It returns a proxy response object generated from the http.ResponseWriter.
@@ -44,6 +51,12 @@ func (e *EchoLambda) ProxyWithContext(ctx context.Context, req events.APIGateway
4451
return e.proxyInternal(echoRequest, err)
4552
}
4653

54+
// ProxyWithContextV2 is the same as ProxyWithContext(), but for V2 payloads instead
55+
func (e *EchoLambda) ProxyWithContextV2(ctx context.Context, req events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
56+
echoRequest, err := e.v2.EventToRequestWithContext(ctx, req)
57+
return e.proxyInternalV2(echoRequest, err)
58+
}
59+
4760
func (e *EchoLambda) proxyInternal(req *http.Request, err error) (events.APIGatewayProxyResponse, error) {
4861

4962
if err != nil {
@@ -60,3 +73,20 @@ func (e *EchoLambda) proxyInternal(req *http.Request, err error) (events.APIGate
6073

6174
return proxyResponse, nil
6275
}
76+
77+
func (e *EchoLambda) proxyInternalV2(req *http.Request, err error) (events.APIGatewayV2HTTPResponse, error) {
78+
79+
if err != nil {
80+
return core.GatewayTimeoutV2(), core.NewLoggedError("Could not convert proxy event to request: %v", err)
81+
}
82+
83+
respWriter := core.NewProxyResponseWriterV2()
84+
e.Echo.ServeHTTP(http.ResponseWriter(respWriter), req)
85+
86+
proxyResponse, err := respWriter.GetProxyResponse()
87+
if err != nil {
88+
return core.GatewayTimeoutV2(), core.NewLoggedError("Error while generating proxy response: %v", err)
89+
}
90+
91+
return proxyResponse, nil
92+
}

0 commit comments

Comments
 (0)