Transform AWS API Gateway Lambda requests and responses to Go HTTP requests and responses.
- 0.x - Current - Under review / development. Candidate for 1.x.
httpadapter
only supports v2 payloads. To handle v1 payloads use therestadapter
.
- Once version 1 has released: maintain the Go compatibility promise as much as possible.
- As few dependencies as possible.
- Looking at you awslabs/aws-lambda-go-api-proxy.
- Currently only a single direct dependency on
github.com/stretchr/testify
for testing.
- Make changes.
- Add / update tests.
- Run
make
to fmt, vet, test, and build your changes. - Commit your changes.
- Submit a PR.
Example Lambda function that transforms the incoming HTTP API request, routes it to a http.ServerMux, and then returns the transformed result. See Example Lambda Function for a working example.
package main
import (
"context"
"net/http"
"net/http/httptest"
"github.com/aws/aws-lambda-go/lambda"
"harrisonhjones.com/go-apigw-http-adapter/httpadapter"
)
func HandleRequest(ctx context.Context, req httpadapter.Request) (*httpadapter.Response, error) {
// FYI: Request transformation.
httpReq, err := httpadapter.TransformRequest(ctx, req)
if err != nil {
return nil, err
}
// FYI: Handle Request.
httpRec := httptest.NewRecorder()
mux := http.NewServeMux()
// TODO: Add your own handlers here.
mux.ServeHTTP(httpRec, httpReq)
// FYI: Response transformation.
httpRes, err := httpadapter.TransformResponse(httpRec.Result(), func(response *http.Response) bool {
// FYI: Here you might inspect the response Content-Type to determine if the response should be encoded or not.
return false // FYI: Don't encode the response.
})
if err != nil {
return nil, err
}
return httpRes, nil
}
func main() {
lambda.Start(HandleRequest)
}
Example Lambda function that transforms the incoming REST API request, routes it to a http.ServerMux, and then returns the transformed result. See Example Lambda Function for a working example.
package main
import (
"context"
"net/http"
"net/http/httptest"
"github.com/aws/aws-lambda-go/lambda"
"harrisonhjones.com/go-apigw-http-adapter/restadapter"
)
func HandleRequest(ctx context.Context, req restadapter.Request) (*restadapter.Response, error) {
// FYI: Request transformation.
httpReq, err := restadapter.TransformRequest(ctx, req)
if err != nil {
return nil, err
}
// FYI: Handle Request.
httpRec := httptest.NewRecorder()
mux := http.NewServeMux()
// TODO: Add your own handlers here.
mux.ServeHTTP(httpRec, httpReq)
// FYI: Response transformation.
httpRes, err := restadapter.TransformResponse(httpRec.Result(), func(response *http.Response) bool {
// FYI: Here you might inspect the response Content-Type to determine if the response should be encoded or not.
return false // FYI: Don't encode the response.
})
if err != nil {
return nil, err
}
return httpRes, nil
}
func main() {
lambda.Start(HandleRequest)
}