forked from awslabs/aws-lambda-go-api-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter.go
62 lines (50 loc) · 2.35 KB
/
adapter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Package ginadapter adds Gin support for the aws-severless-go-api library.
// Uses the core package behind the scenes and exposes the New and NewV2 methods to
// get a new instance and Proxy method to send request to the Gin engine.
package ginadapter
import (
"context"
"net/http"
"github.com/aws/aws-lambda-go/events"
"github.com/awslabs/aws-lambda-go-api-proxy/core"
"github.com/gin-gonic/gin"
)
// GinLambda makes it easy to send API Gateway proxy events to a Gin
// Engine. The library transforms the proxy event into an HTTP request and then
// creates a proxy response object from the http.ResponseWriter
type GinLambda struct {
core.RequestAccessor
ginEngine *gin.Engine
}
// New creates a new instance of the GinLambda object.
// Receives an initialized *gin.Engine object - normally created with gin.Default().
// It returns the initialized instance of the GinLambda object.
func New(gin *gin.Engine) *GinLambda {
return &GinLambda{ginEngine: gin}
}
// Proxy receives an API Gateway proxy event, transforms it into an http.Request
// object, and sends it to the gin.Engine for routing.
// It returns a proxy response object generated from the http.ResponseWriter.
func (g *GinLambda) Proxy(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
ginRequest, err := g.ProxyEventToHTTPRequest(req)
return g.proxyInternal(ginRequest, err)
}
// ProxyWithContext receives context and an API Gateway proxy event,
// transforms them into an http.Request object, and sends it to the gin.Engine for routing.
// It returns a proxy response object generated from the http.ResponseWriter.
func (g *GinLambda) ProxyWithContext(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
ginRequest, err := g.EventToRequestWithContext(ctx, req)
return g.proxyInternal(ginRequest, err)
}
func (g *GinLambda) proxyInternal(req *http.Request, err error) (events.APIGatewayProxyResponse, error) {
if err != nil {
return core.GatewayTimeout(), core.NewLoggedError("Could not convert proxy event to request: %v", err)
}
respWriter := core.NewProxyResponseWriter()
g.ginEngine.ServeHTTP(http.ResponseWriter(respWriter), req)
proxyResponse, err := respWriter.GetProxyResponse()
if err != nil {
return core.GatewayTimeout(), core.NewLoggedError("Error while generating proxy response: %v", err)
}
return proxyResponse, nil
}