|
| 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 | +} |
0 commit comments