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

Commit 431f760

Browse files
authored
Merge pull request #112 from danielwhite/httpadapter-v2-event
Support v2 payloads for `httpadapter`
2 parents 4c15bc8 + 7014900 commit 431f760

File tree

4 files changed

+106
-84
lines changed

4 files changed

+106
-84
lines changed

handlerfunc/adapter.go

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,13 @@
11
package handlerfunc
22

33
import (
4-
"context"
54
"net/http"
65

7-
"github.com/aws/aws-lambda-go/events"
8-
"github.com/awslabs/aws-lambda-go-api-proxy/core"
6+
"github.com/awslabs/aws-lambda-go-api-proxy/httpadapter"
97
)
108

11-
type HandlerFuncAdapter struct {
12-
core.RequestAccessor
13-
handlerFunc http.HandlerFunc
14-
}
9+
type HandlerFuncAdapter = httpadapter.HandlerAdapter
1510

1611
func New(handlerFunc http.HandlerFunc) *HandlerFuncAdapter {
17-
return &HandlerFuncAdapter{
18-
handlerFunc: handlerFunc,
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 *HandlerFuncAdapter) Proxy(event events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, 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.HandlerFunc for routing.
32-
// It returns a proxy response object generated from the http.ResponseWriter.
33-
func (h *HandlerFuncAdapter) ProxyWithContext(ctx context.Context, event events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
34-
req, err := h.EventToRequestWithContext(ctx, event)
35-
return h.proxyInternal(req, err)
36-
}
37-
38-
func (h *HandlerFuncAdapter) proxyInternal(req *http.Request, err error) (events.APIGatewayProxyResponse, error) {
39-
if err != nil {
40-
return core.GatewayTimeout(), core.NewLoggedError("Could not convert proxy event to request: %v", err)
41-
}
42-
43-
w := core.NewProxyResponseWriter()
44-
h.handlerFunc.ServeHTTP(http.ResponseWriter(w), req)
45-
46-
resp, err := w.GetProxyResponse()
47-
if err != nil {
48-
return core.GatewayTimeout(), core.NewLoggedError("Error while generating proxy response: %v", err)
49-
}
50-
51-
return resp, nil
12+
return httpadapter.New(handlerFunc)
5213
}

handlerfunc/adapterv2.go

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,13 @@
11
package handlerfunc
22

33
import (
4-
"context"
54
"net/http"
65

7-
"github.com/aws/aws-lambda-go/events"
8-
"github.com/awslabs/aws-lambda-go-api-proxy/core"
6+
"github.com/awslabs/aws-lambda-go-api-proxy/httpadapter"
97
)
108

11-
type HandlerFuncAdapterV2 struct {
12-
core.RequestAccessorV2
13-
handlerFunc http.HandlerFunc
14-
}
9+
type HandlerFuncAdapterV2 = httpadapter.HandlerAdapterV2
1510

1611
func NewV2(handlerFunc http.HandlerFunc) *HandlerFuncAdapterV2 {
17-
return &HandlerFuncAdapterV2{
18-
handlerFunc: handlerFunc,
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 *HandlerFuncAdapterV2) 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.HandlerFunc for routing.
32-
// It returns a proxy response object generated from the http.ResponseWriter.
33-
func (h *HandlerFuncAdapterV2) 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 *HandlerFuncAdapterV2) 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.handlerFunc.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
12+
return httpadapter.NewV2(handlerFunc)
5213
}

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)