awseventadapter

package module
v0.0.0-...-4f2fc3d Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2019 License: Apache-2.0 Imports: 14 Imported by: 0

README

Currently a work in progress, just a test implementation to determine if the approach works.

This is my attempt reimplement the aws-lambda-go-api-proxy solution to handle both ALBTargetGroupRequest and APIGatewayProxyRequest event scenarios.

I figure this approach has three advantages:

  • Using a superset JSON struct to receive both event types decouples types from the services being consumed. As long as all the fields are present to cast down into the proper event response type.
  • Consuming an http.Handler interface means not having to shim each framework. This does have the disadvantage of requiring the framework expose it's http.Handler interface however, which may or may not be a problem.
  • Using an httptest.Recorder from the stdlib to force the http.Handler to run synchronously means not having to deal with http.CloseNotifier deprecation in the future, and will likely be maintained by the core team.
    • This is not accurate, the httptest.Recorder does not force the request to be synchronous. I've added a channel in an http.Handler as a wrapper with a defer close() to ensure the request has finished processing.

Stuff I'm not sure about:

  • I feel like Go masters would not like using httptest.Recorder in this way, just a gut feeling. But Google uses the http.RoundTripper for OAuth against the docs, sooo, I'm sure it's fine. I suppose if the httptest module assumes it's not used in production there could also be a performance impact over other methods?
  • I don't fully understand the context handling code in the original project. So I pass the original lambda context to the constructed http.Request via .WithContext().

Gotcha's

Why?

I want to be able to use a muxer from gorilla, echo, etc with API Gateway and in my ALB Target Groups. Converts the event into an http.Request, processes it through the framework's http.Handler and returns the proper event response back to API Gateway or ALBTargetGroup.

This is an incomplete POC so far, lots of borrowed code from awslabs/aws-lambda-go-api-proxy to help me get started thinking.

Documentation

Index

Constants

View Source
const (
	// CustomHostVariable is the name of the environment variable that contains
	// the custom hostname for the request. If this variable is not set the framework
	// reverts to `DefaultServerAddress`. The value for a custom host should include
	// a protocol: http://my-custom.host.com
	CustomHostVariable = "GO_API_HOST"

	// DefaultServerAddress is prepended to the path of each incoming reuqest
	DefaultServerAddress = "https://aws-serverless-go-api.com"

	// APIGwContextHeader is the custom header key used to store the
	// API Gateway context. To access the Context properties use the
	// GetAPIGatewayContext method of the RequestAccessor object.
	APIGwContextHeader = "X-GoLambdaProxy-ApiGw-Context"

	// APIGwStageVarsHeader is the custom header key used to store the
	// API Gateway stage variables. To access the stage variable values
	// use the GetAPIGatewayStageVars method of the RequestAccessor object.
	APIGwStageVarsHeader = "X-GoLambdaProxy-ApiGw-StageVars"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AdapterRequest

type AdapterRequest struct {
	Resource                        string              `json:"resource"`
	Path                            string              `json:"path"`
	HTTPMethod                      string              `json:"httpMethod"`
	Headers                         map[string]string   `json:"headers,omitempty"`
	MultiValueHeaders               map[string][]string `json:"multiValueHeaders,omitempty"`
	QueryStringParameters           map[string]string   `json:"queryStringParameters,omitempty"`
	MultiValueQueryStringParameters map[string][]string `json:"multiValueQueryStringParameters,omitempty"`
	PathParameters                  map[string]string   `json:"pathParameters"`
	StageVariables                  map[string]string   `json:"stageVariables"`
	RequestContext                  interface{}         `json:"requestContext"`
	Body                            string              `json:"body"`
	IsBase64Encoded                 bool                `json:"isBase64Encoded,omitempty"`
	// contains filtered or unexported fields
}

AdapterRequest is a struct that contains fields required to produce either an events.APIGatewayResponse or events.ALBTargetGroupResponse

func (*AdapterRequest) Proxy

func (ar *AdapterRequest) Proxy(ctx context.Context, handler http.Handler) (*AdapterResponse, error)

Proxy takes the handler from your flavor of framework and processes it into an AdapterResponse which can be cast to the required event.Response type

func (*AdapterRequest) StripBasePath

func (ar *AdapterRequest) StripBasePath(basePath string) string

StripBasePath used to satisfy base path mappings in API Gateway

func (*AdapterRequest) ToRequest

func (ar *AdapterRequest) ToRequest() (*http.Request, error)

ToRequest converts the AdapterRequest object into an http.Request that can be fed into the framework's http.ServeHTTP method

type AdapterResponse

type AdapterResponse struct {
	StatusCode        int                 `json:"statusCode"`
	StatusDescription string              `json:"statusDescription"`
	Headers           map[string]string   `json:"headers"`
	MultiValueHeaders map[string][]string `json:"multiValueHeaders"`
	Body              string              `json:"body"`
	IsBase64Encoded   bool                `json:"isBase64Encoded,omitempty"`
}

AdapterResponse is a struct that contains fields required to produce either an events.APIGatewayResponse or events.ALBTargetGroupResponse

func NewAdapterResponse

func NewAdapterResponse(r *http.Response) (*AdapterResponse, error)

NewAdapterResponse converts an http.Response into an AdapterResponse

func (*AdapterResponse) ALBTargetGroupResponse

func (ar *AdapterResponse) ALBTargetGroupResponse() (events.ALBTargetGroupResponse, error)

ALBTargetGroupResponse returns an events.ALBTargetGroupResponse from the AdapterResponse

func (*AdapterResponse) APIGatewayProxyResponse

func (ar *AdapterResponse) APIGatewayProxyResponse() (events.APIGatewayProxyResponse, error)

APIGatewayProxyResponse returns an events.APIGatewayProxyResponse from the AdapterResponse

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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