Skip to content
This repository was archived by the owner on May 21, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
})
})
})