apigateway_adapter

package module
v0.0.0-...-59c153b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2021 License: MIT Imports: 8 Imported by: 0

README

Go Api Gateway HTTP Adapter

This library is adapter between AWS Api Gateway event (payload version 2) and standard Go HTTP interface. In concrete terms, it converts github.com/aws/aws-lambda-go/events.APIGatewayV2HTTPRequest to *http.Request and content from http.ResponseWriter back to github.com/aws/aws-lambda-go/events.APIGatewayV2HTTPResponse, which is suitable to be returned from Lambda to API Gateway.

There are number of similar solutions like this out there, but I could not find one for Payload Format Version 2.

Example

At most basic level, this library exposes only one really important function: Adapt.

It takes existing http.Handler, which would be invoked during processing of Lambda event, and returns Lambda handler. Simple example might look like this:

package server

import (
	"net/http"

	"github.com/aws/aws-lambda-go/lambda"
	adapter "github.com/delicb/go-apigateway-http-adapter"
)

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/path", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("hello from lambda"))
	})
	lambda.Start(adapter.Adapt(mux))
}

Lambda handler returned by Adapt method has signature func(ctx context.Context, ev events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error)), where events is github.com/aws/aws-lambda-go/events. It has not been tested or intended to work with non-V2 version of the payload.

*http.Request instance provided to http.Handler will be populated with information obtained from API Gateway request and API Gateway response will be generated from status, headers and body written to http.ResponseWriter. Note that ResponseWriter is dummy object and will not implement things like http.Pusher or http.CloseNotifier.

Additionally, original event can be obtained by using APIGatewayRequest method. For example, to get Authorizer provided by API gateway withing HTTP handler, do the following:

func handler(w http.ResponseWriter, r *http.Request) {
	ev, ok := adapter.APIGatewayRequest(r)
	if ok {
		// do something with authorizer
		// ev.RequestContext.Authorizer
	}
}

Known issues

  • Not everything is covered with tests and existing ones could use more love
  • More advanced logic around base64 response body encoding is needed

Contributing

Feel free to open tickets (or even better, pull requests). I am opened to suggestions.

Licence

Cliware is released under MIT licence.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func APIGatewayRequest

func APIGatewayRequest(req *http.Request) (events.APIGatewayV2HTTPRequest, bool)

APIGatewayRequest returns original APIGatewayV2HTTPRequest event that was used to create *http.Request instance. Second return parameter is flag indicating if event exists attached to the request. If it is false, returned APIGatewayV2HTTPRequest is empty value and should not be consumed.

func Adapt

Adapt returns lambda handler that passes processing to http.Handler.

Adapt converts received API Gateway event to *http.Request instance, invokes handler and converts response to API Gateway response. It only works with version 2 API Gateway integration protocol.

Example usage:

lambda.Start(Adapt(httpServer))

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL