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

Commit 85880e4

Browse files
committed
chiadapter: support API Gateway Events V2
1 parent 2eab254 commit 85880e4

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

chi/adapter_v2.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Package chiadapter add Chi support for the aws-severless-go-api library.
2+
// Uses the core package behind the scenes and exposes the New method to
3+
// get a new instance and Proxy method to send request to the Chi mux.
4+
package chiadapter
5+
6+
import (
7+
"context"
8+
"net/http"
9+
10+
"github.com/aws/aws-lambda-go/events"
11+
"github.com/awslabs/aws-lambda-go-api-proxy/core"
12+
"github.com/go-chi/chi/v5"
13+
)
14+
15+
// ChiLambdaV2 makes it easy to send API Gateway proxy events V2 to a Chi
16+
// Mux. The library transforms the proxy event into an HTTP request and then
17+
// creates a proxy response object from the http.ResponseWriter
18+
type ChiLambdaV2 struct {
19+
core.RequestAccessorV2
20+
21+
chiMux *chi.Mux
22+
}
23+
24+
// NewV2 creates a new instance of the ChiLambdaV2 object.
25+
// Receives an initialized *chi.Mux object - normally created with chi.NewRouter().
26+
// It returns the initialized instance of the ChiLambda object.
27+
func NewV2(chi *chi.Mux) *ChiLambdaV2 {
28+
return &ChiLambdaV2{chiMux: chi}
29+
}
30+
31+
// Proxy receives an API Gateway proxy event V2, transforms it into an http.Request
32+
// object, and sends it to the chi.Mux for routing.
33+
// It returns a proxy response object generated from the http.ResponseWriter.
34+
func (g *ChiLambdaV2) Proxy(req events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
35+
chiRequest, err := g.ProxyEventToHTTPRequest(req)
36+
return g.proxyInternal(chiRequest, err)
37+
}
38+
39+
// ProxyWithContext receives context and an API Gateway proxy event V2,
40+
// transforms them into an http.Request object, and sends it to the chi.Mux for routing.
41+
// It returns a proxy response object generated from the http.ResponseWriter.
42+
func (g *ChiLambdaV2) ProxyWithContext(ctx context.Context, req events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
43+
chiRequest, err := g.EventToRequestWithContext(ctx, req)
44+
return g.proxyInternal(chiRequest, err)
45+
}
46+
47+
func (g *ChiLambdaV2) proxyInternal(chiRequest *http.Request, err error) (events.APIGatewayV2HTTPResponse, error) {
48+
if err != nil {
49+
return core.GatewayTimeoutV2(), core.NewLoggedError("Could not convert proxy event to request: %v", err)
50+
}
51+
52+
respWriter := core.NewProxyResponseWriterV2()
53+
g.chiMux.ServeHTTP(http.ResponseWriter(respWriter), chiRequest)
54+
55+
proxyResponse, err := respWriter.GetProxyResponse()
56+
if err != nil {
57+
return core.GatewayTimeoutV2(), core.NewLoggedError("Error while generating proxy response: %v", err)
58+
}
59+
60+
return proxyResponse, nil
61+
}

chi/chilambda_v2_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package chiadapter_test
2+
3+
import (
4+
"context"
5+
"log"
6+
"net/http"
7+
8+
"github.com/aws/aws-lambda-go/events"
9+
chiadapter "github.com/awslabs/aws-lambda-go-api-proxy/chi"
10+
"github.com/go-chi/chi/v5"
11+
12+
. "github.com/onsi/ginkgo"
13+
. "github.com/onsi/gomega"
14+
)
15+
16+
var _ = Describe("ChiLambda V2 tests", func() {
17+
Context("Simple ping request", func() {
18+
It("Proxies the event correctly", func() {
19+
log.Println("Starting test")
20+
21+
r := chi.NewRouter()
22+
r.Get("/ping", func(w http.ResponseWriter, r *http.Request) {
23+
w.Write([]byte("pong"))
24+
})
25+
26+
adapter := chiadapter.NewV2(r)
27+
28+
req := events.APIGatewayV2HTTPRequest{
29+
RawPath: "/ping",
30+
RequestContext: events.APIGatewayV2HTTPRequestContext{
31+
HTTP: events.APIGatewayV2HTTPRequestContextHTTPDescription{
32+
Method: http.MethodGet,
33+
},
34+
},
35+
}
36+
37+
resp, err := adapter.ProxyWithContext(context.Background(), req)
38+
39+
Expect(err).To(BeNil())
40+
Expect(resp.StatusCode).To(Equal(200))
41+
42+
resp, err = adapter.Proxy(req)
43+
Expect(err).To(BeNil())
44+
Expect(resp.StatusCode).To(Equal(200))
45+
})
46+
})
47+
})

0 commit comments

Comments
 (0)