|
| 1 | +// Package ginadapter adds Gin support for the aws-severless-go-api library. |
| 2 | +// Uses the core package behind the scenes and exposes the New and NewV2 and ALB methods 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 | +// GinLambdaALB makes it easy to send ALB proxy 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 GinLambdaALB struct { |
| 19 | + core.RequestAccessorALB |
| 20 | + |
| 21 | + ginEngine *gin.Engine |
| 22 | +} |
| 23 | + |
| 24 | +// New creates a new instance of the GinLambdaALB object. |
| 25 | +// Receives an initialized *gin.Engine object - normally created with gin.Default(). |
| 26 | +// It returns the initialized instance of the GinLambdaALB object. |
| 27 | +func NewALB(gin *gin.Engine) *GinLambdaALB { |
| 28 | + return &GinLambdaALB{ginEngine: gin} |
| 29 | +} |
| 30 | + |
| 31 | +// Proxy receives an ALB proxy event, transforms it into an http.Request |
| 32 | +// object, and sends it to the gin.Engine for routing. |
| 33 | +// It returns a proxy response object generated from the http.ResponseWriter. |
| 34 | +func (g *GinLambdaALB) Proxy(req events.ALBTargetGroupRequest) (events.ALBTargetGroupResponse, error) { |
| 35 | + ginRequest, err := g.ProxyEventToHTTPRequest(req) |
| 36 | + return g.proxyInternal(ginRequest, err) |
| 37 | +} |
| 38 | + |
| 39 | +// ProxyWithContext receives context and an ALB proxy event, |
| 40 | +// transforms them into an http.Request object, and sends it to the gin.Engine for routing. |
| 41 | +// It returns a proxy response object generated from the http.ResponseWriter. |
| 42 | +func (g *GinLambdaALB) ProxyWithContext(ctx context.Context, req events.ALBTargetGroupRequest) (events.ALBTargetGroupResponse, error) { |
| 43 | + ginRequest, err := g.EventToRequestWithContext(ctx, req) |
| 44 | + return g.proxyInternal(ginRequest, err) |
| 45 | +} |
| 46 | + |
| 47 | +func (g *GinLambdaALB) proxyInternal(req *http.Request, err error) (events.ALBTargetGroupResponse, error) { |
| 48 | + |
| 49 | + if err != nil { |
| 50 | + return core.GatewayTimeoutALB(), core.NewLoggedError("Could not convert proxy event to request: %v", err) |
| 51 | + } |
| 52 | + |
| 53 | + respWriter := core.NewProxyResponseWriterALB() |
| 54 | + g.ginEngine.ServeHTTP(http.ResponseWriter(respWriter), req) |
| 55 | + |
| 56 | + proxyResponse, err := respWriter.GetProxyResponse() |
| 57 | + if err != nil { |
| 58 | + return core.GatewayTimeoutALB(), core.NewLoggedError("Error while generating proxy response: %v", err) |
| 59 | + } |
| 60 | + |
| 61 | + return proxyResponse, nil |
| 62 | +} |
0 commit comments