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