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

Commit b7f48d1

Browse files
committed
Add support for ApiGateway V2 version for the gorilla/mux adapter
1 parent 686c88b commit b7f48d1

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

gorillamux/adapterv2.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package gorillamux
2+
3+
import (
4+
"context"
5+
"net/http"
6+
7+
"github.com/aws/aws-lambda-go/events"
8+
"github.com/awslabs/aws-lambda-go-api-proxy/core"
9+
"github.com/gorilla/mux"
10+
)
11+
12+
type GorillaMuxAdapterV2 struct {
13+
core.RequestAccessorV2
14+
router *mux.Router
15+
}
16+
17+
func NewV2(router *mux.Router) *GorillaMuxAdapterV2 {
18+
return &GorillaMuxAdapterV2{
19+
router: router,
20+
}
21+
}
22+
23+
// Proxy receives an API Gateway proxy event, transforms it into an http.Request
24+
// object, and sends it to the mux.Router for routing.
25+
// It returns a proxy response object generated from the http.ResponseWriter.
26+
func (h *GorillaMuxAdapterV2) Proxy(event events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
27+
req, err := h.ProxyEventToHTTPRequest(event)
28+
return h.proxyInternal(req, err)
29+
}
30+
31+
// ProxyWithContext receives context and an API Gateway proxy event,
32+
// transforms them into an http.Request object, and sends it to the mux.Router for routing.
33+
// It returns a proxy response object generated from the http.ResponseWriter.
34+
func (h *GorillaMuxAdapterV2) ProxyWithContext(ctx context.Context, event events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
35+
req, err := h.EventToRequestWithContext(ctx, event)
36+
return h.proxyInternal(req, err)
37+
}
38+
39+
func (h *GorillaMuxAdapterV2) proxyInternal(req *http.Request, err error) (events.APIGatewayV2HTTPResponse, error) {
40+
if err != nil {
41+
return core.GatewayTimeoutV2(), core.NewLoggedError("Could not convert proxy event to request: %v", err)
42+
}
43+
44+
w := core.NewProxyResponseWriterV2()
45+
h.router.ServeHTTP(http.ResponseWriter(w), req)
46+
47+
resp, err := w.GetProxyResponse()
48+
if err != nil {
49+
return core.GatewayTimeoutV2(), core.NewLoggedError("Error while generating proxy response: %v", err)
50+
}
51+
52+
return resp, nil
53+
}

gorillamux/adapterv2_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package gorillamux_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
8+
"github.com/aws/aws-lambda-go/events"
9+
"github.com/awslabs/aws-lambda-go-api-proxy/gorillamux"
10+
"github.com/gorilla/mux"
11+
12+
. "github.com/onsi/ginkgo"
13+
. "github.com/onsi/gomega"
14+
)
15+
16+
var _ = Describe("GorillaMuxAdapterV2 tests", func() {
17+
Context("Simple ping request", func() {
18+
It("Proxies the event correctly", func() {
19+
homeHandler := func(w http.ResponseWriter, req *http.Request) {
20+
w.Header().Add("unfortunately-required-header", "")
21+
fmt.Fprintf(w, "Home Page")
22+
}
23+
24+
productsHandler := func(w http.ResponseWriter, req *http.Request) {
25+
w.Header().Add("unfortunately-required-header", "")
26+
fmt.Fprintf(w, "Products Page")
27+
}
28+
29+
r := mux.NewRouter()
30+
r.HandleFunc("/", homeHandler)
31+
r.HandleFunc("/products", productsHandler)
32+
33+
adapter := gorillamux.NewV2(r)
34+
35+
homePageReq := events.APIGatewayV2HTTPRequest{
36+
RequestContext: events.APIGatewayV2HTTPRequestContext{
37+
HTTP: events.APIGatewayV2HTTPRequestContextHTTPDescription{
38+
Method: http.MethodGet,
39+
Path: "/",
40+
},
41+
},
42+
}
43+
44+
homePageResp, homePageReqErr := adapter.ProxyWithContext(context.Background(), homePageReq)
45+
46+
Expect(homePageReqErr).To(BeNil())
47+
Expect(homePageResp.StatusCode).To(Equal(200))
48+
Expect(homePageResp.Body).To(Equal("Home Page"))
49+
50+
productsPageReq := events.APIGatewayV2HTTPRequest{
51+
RequestContext: events.APIGatewayV2HTTPRequestContext{
52+
HTTP: events.APIGatewayV2HTTPRequestContextHTTPDescription{
53+
Method: http.MethodGet,
54+
Path: "/products",
55+
},
56+
},
57+
}
58+
59+
productsPageResp, productsPageReqErr := adapter.Proxy(productsPageReq)
60+
61+
Expect(productsPageReqErr).To(BeNil())
62+
Expect(productsPageResp.StatusCode).To(Equal(200))
63+
Expect(productsPageResp.Body).To(Equal("Products Page"))
64+
})
65+
})
66+
})

0 commit comments

Comments
 (0)