Skip to content

Deserialize/serialize AWS API Gateway Lambda requests/responses to Go HTTP requests/responses

License

Notifications You must be signed in to change notification settings

harrisonhjones/go-apigw-http-adapter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-apigw-http-adapter

Transform AWS API Gateway Lambda requests and responses to Go HTTP requests and responses.

Badges

  • Build: Build
  • Report Card: Go Report Card

Releases

  • 0.x - Current - Under review / development. Candidate for 1.x.

Links

Limitations

  1. httpadapter only supports v2 payloads. To handle v1 payloads use the restadapter.

Goals

  1. Once version 1 has released: maintain the Go compatibility promise as much as possible.
  2. As few dependencies as possible.
    1. Looking at you awslabs/aws-lambda-go-api-proxy.
    2. Currently only a single direct dependency on github.com/stretchr/testify for testing.

Contributing

  1. Make changes.
  2. Add / update tests.
  3. Run make to fmt, vet, test, and build your changes.
  4. Commit your changes.
  5. Submit a PR.

HTTP Adapter Lambda Example

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)
}

REST Adapter Lambda Example

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)
}

About

Deserialize/serialize AWS API Gateway Lambda requests/responses to Go HTTP requests/responses

Resources

License

Stars

Watchers

Forks

Packages

No packages published