httpadapt

package module
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2020 License: MIT Imports: 13 Imported by: 0

README

httpadapt

Adapt a http.Handler to handle API Gateway Proxy events in AWS Lambda. This library started as simple fork of the blessed https://github.com/awslabs/aws-lambda-go-api-proxy library but in the end so much was changed that only very little of the original codebase remains.

features

  • This library only depends on the standard library and github.com/aws/aws-lambda-go
  • We removed all support for non-context based HTT handling to vastly simply the code base
  • Only supports the standard http.Handler interface to vastly simplify the code
  • Query parameters aren now ordered deterministicly instead
  • Instead of writing our own ResponseWriter we use the battle tested httptest.ResponseRecorder
  • Well tested with coverage of over 95%

usage

package main

import (
	"fmt"
	"net/http"

	"github.com/aws/aws-lambda-go/lambda"
	"github.com/gohandle/httpadapt"
)

func handle(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "hello, %v", r.URL)
}

func main() {
	lambda.Start(
		httpadapt.New(http.HandlerFunc(handle)).ProxyWithContext)
}

backlog

  • Add a functional option to configure stripbasepath
  • Add a functional option for CustomHostVariable env
  • Test errors, possiblty with a package error type
  • Consider the v2 api format
  • Prevent header from being edited after writing with Write, else it will work on lambda but not in a real server

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAPIGatewayContextFromContext added in v0.9.1

func GetAPIGatewayContextFromContext(ctx context.Context) (events.APIGatewayProxyRequestContext, bool)

GetAPIGatewayContextFromContext retrieve APIGatewayProxyRequestContext from context.Context

func GetRuntimeContextFromContext added in v0.9.1

func GetRuntimeContextFromContext(ctx context.Context) (*lambdacontext.LambdaContext, bool)

GetRuntimeContextFromContext retrieve Lambda Runtime Context from context.Context

func GetStageVarsFromContext added in v0.9.1

func GetStageVarsFromContext(ctx context.Context) (map[string]string, bool)

GetStageVarsFromContext retrieve stage variables from context

func ProxyResponse

func ProxyResponse(rec *httptest.ResponseRecorder) (out events.APIGatewayProxyResponse)

ProxyResponse returns the resulting response to handling the API Gateway Proxy event

func WithContext added in v0.9.2

func WithContext(ctx context.Context, apiGwRequest events.APIGatewayProxyRequest) context.Context

WithContext returns a context with gateway context and lamda context added (if any)

Types

type Adapter

type Adapter struct {
	// contains filtered or unexported fields
}

Adapter adapts http.Handler implementations.

func New

func New(h http.Handler, opts ...Option) (a *Adapter)

New inits an adapter

func (*Adapter) EventToRequest

func (a *Adapter) EventToRequest(ev events.APIGatewayProxyRequest) (req *http.Request, err error)

EventToRequest converts an API Gateway proxy event into an http.Request object. Returns the populated request maintaining headers

func (*Adapter) ProxyWithContext

func (a *Adapter) ProxyWithContext(
	ctx context.Context,
	ev events.APIGatewayProxyRequest,
) (out events.APIGatewayProxyResponse, err error)

ProxyWithContext receives context and an API Gateway proxy event, transforms them into an http.Request object, and sends it to the http.Handler for routing. It returns a proxy response object generated from the http.ResponseWriter.

type Option

type Option func(a *Adapter)

Option configures the adapter

func CustomHost

func CustomHost(host string) Option

CustomHost configures the custom hostname for the request. If this option is not set the framework reverts to `RequestContext.DomainName`. The value for a custom host should include a protocol: http://my-custom.host.com

func StripBasePath

func StripBasePath(base string) Option

StripBasePath configures the adapter to strip the prefixing part of the resulting url path

Jump to

Keyboard shortcuts

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