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

Commit 83f57c9

Browse files
committed
fiber proxy for api gateway http payload v2
1 parent 15faf8c commit 83f57c9

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

fiber/adapter.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
// creates a proxy response object from the *fiber.Ctx
2222
type FiberLambda struct {
2323
core.RequestAccessor
24+
v2 core.RequestAccessorV2
2425
app *fiber.App
2526
}
2627

@@ -41,6 +42,12 @@ func (f *FiberLambda) Proxy(req events.APIGatewayProxyRequest) (events.APIGatewa
4142
return f.proxyInternal(fiberRequest, err)
4243
}
4344

45+
// ProxyV2 is just same as Proxy() but for APIGateway HTTP payload v2
46+
func (f *FiberLambda) ProxyV2(req events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
47+
fiberRequest, err := f.v2.ProxyEventToHTTPRequest(req)
48+
return f.proxyInternalV2(fiberRequest, err)
49+
}
50+
4451
// ProxyWithContext receives context and an API Gateway proxy event,
4552
// transforms them into an http.Request object, and sends it to the echo.Echo for routing.
4653
// It returns a proxy response object generated from the http.ResponseWriter.
@@ -49,6 +56,12 @@ func (f *FiberLambda) ProxyWithContext(ctx context.Context, req events.APIGatewa
4956
return f.proxyInternal(fiberRequest, err)
5057
}
5158

59+
// ProxyWithContextV2 is just same as ProxyWithContext() but for APIGateway HTTP payload v2
60+
func (f *FiberLambda) ProxyWithContextV2(ctx context.Context, req events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
61+
fiberRequest, err := f.v2.EventToRequestWithContext(ctx, req)
62+
return f.proxyInternalV2(fiberRequest, err)
63+
}
64+
5265
func (f *FiberLambda) proxyInternal(req *http.Request, err error) (events.APIGatewayProxyResponse, error) {
5366

5467
if err != nil {
@@ -66,6 +79,23 @@ func (f *FiberLambda) proxyInternal(req *http.Request, err error) (events.APIGat
6679
return proxyResponse, nil
6780
}
6881

82+
func (f *FiberLambda) proxyInternalV2(req *http.Request, err error) (events.APIGatewayV2HTTPResponse, error) {
83+
84+
if err != nil {
85+
return core.GatewayTimeoutV2(), core.NewLoggedError("Could not convert proxy event to request: %v", err)
86+
}
87+
88+
resp := core.NewProxyResponseWriterV2()
89+
f.adaptor(resp, req)
90+
91+
proxyResponse, err := resp.GetProxyResponse()
92+
if err != nil {
93+
return core.GatewayTimeoutV2(), core.NewLoggedError("Error while generating proxy response: %v", err)
94+
}
95+
96+
return proxyResponse, nil
97+
}
98+
6999
func (f *FiberLambda) adaptor(w http.ResponseWriter, r *http.Request) {
70100
// New fasthttp request
71101
req := fasthttp.AcquireRequest()

0 commit comments

Comments
 (0)