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

Commit 2c6c363

Browse files
committed
httpadapter: Add support for proxying events.APIGatewayV2HTTPRequest
This provides an equivalent implementation to `handlerfunc.HandlerFuncAdapterV2` that will proxy `events.APIGatewayV2HTTPRequest` to a `http.Handler`.
1 parent 0f07840 commit 2c6c363

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

httpadapter/adapterv2.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package httpadapter
2+
3+
import (
4+
"context"
5+
"net/http"
6+
7+
"github.com/aws/aws-lambda-go/events"
8+
"github.com/awslabs/aws-lambda-go-api-proxy/core"
9+
)
10+
11+
type HandlerAdapterV2 struct {
12+
core.RequestAccessorV2
13+
handler http.Handler
14+
}
15+
16+
func NewV2(handler http.Handler) *HandlerAdapterV2 {
17+
return &HandlerAdapterV2{
18+
handler: handler,
19+
}
20+
}
21+
22+
// Proxy receives an API Gateway proxy event, transforms it into an http.Request
23+
// object, and sends it to the http.HandlerFunc for routing.
24+
// It returns a proxy response object generated from the http.ResponseWriter.
25+
func (h *HandlerAdapterV2) Proxy(event events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
26+
req, err := h.ProxyEventToHTTPRequest(event)
27+
return h.proxyInternal(req, err)
28+
}
29+
30+
// ProxyWithContext receives context and an API Gateway proxy event,
31+
// transforms them into an http.Request object, and sends it to the http.Handler for routing.
32+
// It returns a proxy response object generated from the http.ResponseWriter.
33+
func (h *HandlerAdapterV2) ProxyWithContext(ctx context.Context, event events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
34+
req, err := h.EventToRequestWithContext(ctx, event)
35+
return h.proxyInternal(req, err)
36+
}
37+
38+
func (h *HandlerAdapterV2) proxyInternal(req *http.Request, err error) (events.APIGatewayV2HTTPResponse, error) {
39+
if err != nil {
40+
return core.GatewayTimeoutV2(), core.NewLoggedError("Could not convert proxy event to request: %v", err)
41+
}
42+
43+
w := core.NewProxyResponseWriterV2()
44+
h.handler.ServeHTTP(http.ResponseWriter(w), req)
45+
46+
resp, err := w.GetProxyResponse()
47+
if err != nil {
48+
return core.GatewayTimeoutV2(), core.NewLoggedError("Error while generating proxy response: %v", err)
49+
}
50+
51+
return resp, nil
52+
}

httpadapter/adapterv2_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package httpadapter_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"net/http"
8+
9+
"github.com/aws/aws-lambda-go/events"
10+
"github.com/awslabs/aws-lambda-go-api-proxy/httpadapter"
11+
12+
. "github.com/onsi/ginkgo"
13+
. "github.com/onsi/gomega"
14+
)
15+
16+
var _ = Describe("HandlerFuncAdapter tests", func() {
17+
Context("Simple ping request", func() {
18+
It("Proxies the event correctly", func() {
19+
log.Println("Starting test")
20+
21+
var handler http.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
22+
w.Header().Add("unfortunately-required-header", "")
23+
fmt.Fprintf(w, "Go Lambda!!")
24+
})
25+
26+
adapter := httpadapter.NewV2(handler)
27+
28+
req := events.APIGatewayV2HTTPRequest{
29+
RequestContext: events.APIGatewayV2HTTPRequestContext{
30+
HTTP: events.APIGatewayV2HTTPRequestContextHTTPDescription{
31+
Method: http.MethodGet,
32+
Path: "/ping",
33+
},
34+
},
35+
}
36+
37+
resp, err := adapter.ProxyWithContext(context.Background(), req)
38+
39+
Expect(err).To(BeNil())
40+
Expect(resp.StatusCode).To(Equal(200))
41+
42+
resp, err = adapter.Proxy(req)
43+
44+
Expect(err).To(BeNil())
45+
Expect(resp.StatusCode).To(Equal(200))
46+
})
47+
})
48+
})

0 commit comments

Comments
 (0)