|
1 | 1 | package handlerfunc
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "context" |
5 | 4 | "net/http"
|
6 | 5 |
|
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" |
9 | 7 | )
|
10 | 8 |
|
11 |
| -type HandlerFuncAdapterV2 struct { |
12 |
| - core.RequestAccessorV2 |
13 |
| - handlerFunc http.HandlerFunc |
14 |
| -} |
| 9 | +type HandlerFuncAdapterV2 = httpadapter.HandlerAdapterV2 |
15 | 10 |
|
16 | 11 | 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) |
52 | 13 | }
|
0 commit comments