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

Add support for ApiGateway V2 version for the gorilla/mux adapter #105

Merged
merged 1 commit into from
Dec 21, 2021
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
53 changes: 53 additions & 0 deletions gorillamux/adapterv2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package gorillamux

import (
"context"
"net/http"

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

type GorillaMuxAdapterV2 struct {
core.RequestAccessorV2
router *mux.Router
}

func NewV2(router *mux.Router) *GorillaMuxAdapterV2 {
return &GorillaMuxAdapterV2{
router: router,
}
}

// Proxy receives an API Gateway proxy event, transforms it into an http.Request
// object, and sends it to the mux.Router for routing.
// It returns a proxy response object generated from the http.ResponseWriter.
func (h *GorillaMuxAdapterV2) 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 mux.Router for routing.
// It returns a proxy response object generated from the http.ResponseWriter.
func (h *GorillaMuxAdapterV2) ProxyWithContext(ctx context.Context, event events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
req, err := h.EventToRequestWithContext(ctx, event)
return h.proxyInternal(req, err)
}

func (h *GorillaMuxAdapterV2) 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.router.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
}
66 changes: 66 additions & 0 deletions gorillamux/adapterv2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package gorillamux_test

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

"github.com/aws/aws-lambda-go/events"
"github.com/awslabs/aws-lambda-go-api-proxy/gorillamux"
"github.com/gorilla/mux"

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

var _ = Describe("GorillaMuxAdapterV2 tests", func() {
Context("Simple ping request", func() {
It("Proxies the event correctly", func() {
homeHandler := func(w http.ResponseWriter, req *http.Request) {
w.Header().Add("unfortunately-required-header", "")
fmt.Fprintf(w, "Home Page")
}

productsHandler := func(w http.ResponseWriter, req *http.Request) {
w.Header().Add("unfortunately-required-header", "")
fmt.Fprintf(w, "Products Page")
}

r := mux.NewRouter()
r.HandleFunc("/", homeHandler)
r.HandleFunc("/products", productsHandler)

adapter := gorillamux.NewV2(r)

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

homePageResp, homePageReqErr := adapter.ProxyWithContext(context.Background(), homePageReq)

Expect(homePageReqErr).To(BeNil())
Expect(homePageResp.StatusCode).To(Equal(200))
Expect(homePageResp.Body).To(Equal("Home Page"))

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

productsPageResp, productsPageReqErr := adapter.Proxy(productsPageReq)

Expect(productsPageReqErr).To(BeNil())
Expect(productsPageResp.StatusCode).To(Equal(200))
Expect(productsPageResp.Body).To(Equal("Products Page"))
})
})
})