Skip to content

Commit

Permalink
Merge pull request #112 from danielwhite/httpadapter-v2-event
Browse files Browse the repository at this point in the history
Support v2 payloads for `httpadapter`
  • Loading branch information
sapessi authored Dec 21, 2021
2 parents 4c15bc8 + 7014900 commit 431f760
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 84 deletions.
45 changes: 3 additions & 42 deletions handlerfunc/adapter.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,13 @@
package handlerfunc

import (
"context"
"net/http"

"github.com/aws/aws-lambda-go/events"
"github.com/awslabs/aws-lambda-go-api-proxy/core"
"github.com/awslabs/aws-lambda-go-api-proxy/httpadapter"
)

type HandlerFuncAdapter struct {
core.RequestAccessor
handlerFunc http.HandlerFunc
}
type HandlerFuncAdapter = httpadapter.HandlerAdapter

func New(handlerFunc http.HandlerFunc) *HandlerFuncAdapter {
return &HandlerFuncAdapter{
handlerFunc: handlerFunc,
}
}

// Proxy receives an API Gateway proxy event, transforms it into an http.Request
// object, and sends it to the http.HandlerFunc for routing.
// It returns a proxy response object generated from the http.ResponseWriter.
func (h *HandlerFuncAdapter) Proxy(event events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
req, err := h.ProxyEventToHTTPRequest(event)
return h.proxyInternal(req, err)
}

// ProxyWithContext receives context and an API Gateway proxy event,
// transforms them into an http.Request object, and sends it to the http.HandlerFunc for routing.
// It returns a proxy response object generated from the http.ResponseWriter.
func (h *HandlerFuncAdapter) ProxyWithContext(ctx context.Context, event events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
req, err := h.EventToRequestWithContext(ctx, event)
return h.proxyInternal(req, err)
}

func (h *HandlerFuncAdapter) proxyInternal(req *http.Request, err error) (events.APIGatewayProxyResponse, error) {
if err != nil {
return core.GatewayTimeout(), core.NewLoggedError("Could not convert proxy event to request: %v", err)
}

w := core.NewProxyResponseWriter()
h.handlerFunc.ServeHTTP(http.ResponseWriter(w), req)

resp, err := w.GetProxyResponse()
if err != nil {
return core.GatewayTimeout(), core.NewLoggedError("Error while generating proxy response: %v", err)
}

return resp, nil
return httpadapter.New(handlerFunc)
}
45 changes: 3 additions & 42 deletions handlerfunc/adapterv2.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,13 @@
package handlerfunc

import (
"context"
"net/http"

"github.com/aws/aws-lambda-go/events"
"github.com/awslabs/aws-lambda-go-api-proxy/core"
"github.com/awslabs/aws-lambda-go-api-proxy/httpadapter"
)

type HandlerFuncAdapterV2 struct {
core.RequestAccessorV2
handlerFunc http.HandlerFunc
}
type HandlerFuncAdapterV2 = httpadapter.HandlerAdapterV2

func NewV2(handlerFunc http.HandlerFunc) *HandlerFuncAdapterV2 {
return &HandlerFuncAdapterV2{
handlerFunc: handlerFunc,
}
}

// Proxy receives an API Gateway proxy event, transforms it into an http.Request
// object, and sends it to the http.HandlerFunc for routing.
// It returns a proxy response object generated from the http.ResponseWriter.
func (h *HandlerFuncAdapterV2) Proxy(event events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
req, err := h.ProxyEventToHTTPRequest(event)
return h.proxyInternal(req, err)
}

// ProxyWithContext receives context and an API Gateway proxy event,
// transforms them into an http.Request object, and sends it to the http.HandlerFunc for routing.
// It returns a proxy response object generated from the http.ResponseWriter.
func (h *HandlerFuncAdapterV2) ProxyWithContext(ctx context.Context, event events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
req, err := h.EventToRequestWithContext(ctx, event)
return h.proxyInternal(req, err)
}

func (h *HandlerFuncAdapterV2) proxyInternal(req *http.Request, err error) (events.APIGatewayV2HTTPResponse, error) {
if err != nil {
return core.GatewayTimeoutV2(), core.NewLoggedError("Could not convert proxy event to request: %v", err)
}

w := core.NewProxyResponseWriterV2()
h.handlerFunc.ServeHTTP(http.ResponseWriter(w), req)

resp, err := w.GetProxyResponse()
if err != nil {
return core.GatewayTimeoutV2(), core.NewLoggedError("Error while generating proxy response: %v", err)
}

return resp, nil
return httpadapter.NewV2(handlerFunc)
}
52 changes: 52 additions & 0 deletions httpadapter/adapterv2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package httpadapter

import (
"context"
"net/http"

"github.com/aws/aws-lambda-go/events"
"github.com/awslabs/aws-lambda-go-api-proxy/core"
)

type HandlerAdapterV2 struct {
core.RequestAccessorV2
handler http.Handler
}

func NewV2(handler http.Handler) *HandlerAdapterV2 {
return &HandlerAdapterV2{
handler: handler,
}
}

// Proxy receives an API Gateway proxy event, transforms it into an http.Request
// object, and sends it to the http.HandlerFunc for routing.
// It returns a proxy response object generated from the http.ResponseWriter.
func (h *HandlerAdapterV2) Proxy(event events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
req, err := h.ProxyEventToHTTPRequest(event)
return h.proxyInternal(req, err)
}

// ProxyWithContext receives context and an API Gateway proxy event,
// transforms them into an http.Request object, and sends it to the http.Handler for routing.
// It returns a proxy response object generated from the http.ResponseWriter.
func (h *HandlerAdapterV2) ProxyWithContext(ctx context.Context, event events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
req, err := h.EventToRequestWithContext(ctx, event)
return h.proxyInternal(req, err)
}

func (h *HandlerAdapterV2) proxyInternal(req *http.Request, err error) (events.APIGatewayV2HTTPResponse, error) {
if err != nil {
return core.GatewayTimeoutV2(), core.NewLoggedError("Could not convert proxy event to request: %v", err)
}

w := core.NewProxyResponseWriterV2()
h.handler.ServeHTTP(http.ResponseWriter(w), req)

resp, err := w.GetProxyResponse()
if err != nil {
return core.GatewayTimeoutV2(), core.NewLoggedError("Error while generating proxy response: %v", err)
}

return resp, nil
}
48 changes: 48 additions & 0 deletions httpadapter/adapterv2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package httpadapter_test

import (
"context"
"fmt"
"log"
"net/http"

"github.com/aws/aws-lambda-go/events"
"github.com/awslabs/aws-lambda-go-api-proxy/httpadapter"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("HandlerFuncAdapter tests", func() {
Context("Simple ping request", func() {
It("Proxies the event correctly", func() {
log.Println("Starting test")

var handler http.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Add("unfortunately-required-header", "")
fmt.Fprintf(w, "Go Lambda!!")
})

adapter := httpadapter.NewV2(handler)

req := events.APIGatewayV2HTTPRequest{
RequestContext: events.APIGatewayV2HTTPRequestContext{
HTTP: events.APIGatewayV2HTTPRequestContextHTTPDescription{
Method: http.MethodGet,
Path: "/ping",
},
},
}

resp, err := adapter.ProxyWithContext(context.Background(), req)

Expect(err).To(BeNil())
Expect(resp.StatusCode).To(Equal(200))

resp, err = adapter.Proxy(req)

Expect(err).To(BeNil())
Expect(resp.StatusCode).To(Equal(200))
})
})
})

0 comments on commit 431f760

Please sign in to comment.