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

Commit 7014900

Browse files
committed
handlerfunc: Implement v1 in terms of httpadapter package
A `http.HandlerFunc` implements the `http.Handler` interface so this package is redundant. To avoid breaking backwards compatibility, this uses type aliases and preserves the `handlerfunc.New` and `handlerfunc.NewV2` methods.
1 parent 2c6c363 commit 7014900

File tree

2 files changed

+6
-84
lines changed

2 files changed

+6
-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
}

0 commit comments

Comments
 (0)