Skip to content

Commit d543ebf

Browse files
committed
feat(gin): Add support for ApiGateway V2 version for the Gin adapter
1 parent fc8c062 commit d543ebf

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

gin/adapterv2.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Package ginadapterv2 adds Gin support for the aws-severless-go-api library.
2+
// Uses the core package behind the scenes and exposes the NewV2 method to
3+
// get a new instance and Proxy method to send request to the Gin engine.
4+
package ginadapter
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/gin-gonic/gin"
13+
)
14+
15+
// GinLambdaV2 makes it easy to send API Gateway proxy V2 events to a Gin
16+
// Engine. The library transforms the proxy event into an HTTP request and then
17+
// creates a proxy response object from the http.ResponseWriter
18+
type GinLambdaV2 struct {
19+
core.RequestAccessorV2
20+
21+
ginEngine *gin.Engine
22+
}
23+
24+
// NewV2 creates a new instance of the GinLambdaV2 object.
25+
// Receives an initialized *gin.Engine object - normally created with gin.Default().
26+
// It returns the initialized instance of the GinLambdaV2 object.
27+
func NewV2(gin *gin.Engine) *GinLambdaV2 {
28+
return &GinLambdaV2{ginEngine: gin}
29+
}
30+
31+
// Proxy receives an API Gateway proxy V2 event, transforms it into an http.Request
32+
// object, and sends it to the gin.Engine for routing.
33+
// It returns an http response object generated from the http.ResponseWriter.
34+
func (g *GinLambdaV2) Proxy(req events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
35+
ginRequest, err := g.ProxyEventToHTTPRequest(req)
36+
return g.proxyInternal(ginRequest, err)
37+
}
38+
39+
// ProxyWithContext receives context and an API Gateway proxy V2 event,
40+
// transforms them into an http.Request object, and sends it to the gin.Engine for routing.
41+
// It returns an http response object generated from the http.ResponseWriter.
42+
func (g *GinLambdaV2) ProxyWithContext(ctx context.Context, req events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
43+
ginRequest, err := g.EventToRequestWithContext(ctx, req)
44+
return g.proxyInternal(ginRequest, err)
45+
}
46+
47+
func (g *GinLambdaV2) proxyInternal(req *http.Request, err error) (events.APIGatewayV2HTTPResponse, error) {
48+
49+
if err != nil {
50+
return core.GatewayTimeoutV2(), core.NewLoggedError("Could not convert proxy event to request: %v", err)
51+
}
52+
53+
respWriter := core.NewProxyResponseWriterV2()
54+
g.ginEngine.ServeHTTP(http.ResponseWriter(respWriter), req)
55+
56+
proxyResponse, err := respWriter.GetProxyResponse()
57+
if err != nil {
58+
return core.GatewayTimeoutV2(), core.NewLoggedError("Error while generating proxy response: %v", err)
59+
}
60+
61+
return proxyResponse, nil
62+
}

gin/ginlambda_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,39 @@ var _ = Describe("GinLambda tests", func() {
4343
})
4444
})
4545
})
46+
47+
var _ = Describe("GinLambdaV2 tests", func() {
48+
Context("Simple ping request", func() {
49+
It("Proxies the event correctly", func() {
50+
log.Println("Starting test")
51+
r := gin.Default()
52+
r.GET("/ping", func(c *gin.Context) {
53+
log.Println("Handler!!")
54+
c.JSON(200, gin.H{
55+
"message": "pong",
56+
})
57+
})
58+
59+
adapter := ginadapter.NewV2(r)
60+
61+
req := events.APIGatewayV2HTTPRequest{
62+
RequestContext: events.APIGatewayV2HTTPRequestContext{
63+
HTTP: events.APIGatewayV2HTTPRequestContextHTTPDescription{
64+
Method: "GET",
65+
Path: "/ping",
66+
},
67+
},
68+
}
69+
70+
resp, err := adapter.ProxyWithContext(context.Background(), req)
71+
72+
Expect(err).To(BeNil())
73+
Expect(resp.StatusCode).To(Equal(200))
74+
75+
resp, err = adapter.Proxy(req)
76+
77+
Expect(err).To(BeNil())
78+
Expect(resp.StatusCode).To(Equal(200))
79+
})
80+
})
81+
})

0 commit comments

Comments
 (0)