lambdaproxyhttpadapter

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2023 License: MIT Imports: 6 Imported by: 1

README

Lambda Proxy HTTP Adapter

Build Status

Wrap your API Gateway Proxy Lambda handlers in a go net/http handler. Useful to make the Lambda Proxy handlers compatible with the huge amount of tooling that already exists for net/http.

This adapter will work for many use cases, but was built specifically with https://github.com/steinfletcher/apitest in mind. This adapter allows you to write API tests for your AWS API Gateway Proxy Lambda applications.

Usage

Basic
package main

import (
	"github.com/aws/aws-lambda-go/events"
	"github.com/jfallis/lambda-proxy-http-adapter"
)

func main() {
	httpHandler := lambdaproxyhttpadapter.GetHTTPHandler(apiGatewayProxyHandler, "/", nil, nil)
	...
}

func apiGatewayProxyHandler(r events.APIGatewayProxyRequest) (any, error) {
	...
}
With Path Params

When using API Gateway, path routing is done on behalf of the application inside the API Gateway. Because of this, if your application uses path params, you need to pass the resource path pattern. This is so the adapter is able to parse the URL in the request, and is able to provide path params to your proxy handler.

package main

import (
	"github.com/aws/aws-lambda-go/events"
	"github.com/jfallis/lambda-proxy-http-adapter"
)

func main() {
	httpHandler := lambdaproxyhttpadapter.GetHTTPHandler(apiGatewayProxyHandler, "/users/{userId}", nil, nil)
	...
}

func apiGatewayProxyHandler(r events.APIGatewayProxyRequest) (any, error) {
	...
}
With Stage Variables

API Gateway Proxy handlers allows you to use stage variables. If you use these in your handler, you have to pass them into the adapter as a map[string]string.

package main

import (
	"github.com/aws/aws-lambda-go/events"
	"github.com/jfallis/lambda-proxy-http-adapter"
)

func main() {
	httpHandler := lambdaproxyhttpadapter.GetHTTPHandler(apiGatewayProxyHandler, "/", map[string]string{"var1": "var1value"}, nil)
	...
}

func apiGatewayProxyHandler(r events.APIGatewayProxyRequest) (any, error) {
	...
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func APIGatewayProxyRequestAdaptor added in v0.4.0

func APIGatewayProxyRequestAdaptor(
	r *http.Request,
	body,
	resourcePathPattern string,
	stageVariables map[string]string,
	requestContext *events.APIGatewayProxyRequestContext,
) events.APIGatewayProxyRequest

func GetHTTPHandler added in v0.3.0

func GetHTTPHandler(
	lambdaHandler APIGatewayProxyHandler,
	resourcePathPattern string,
	stageVariables map[string]string,
	proxyRequestCtx *events.APIGatewayProxyRequestContext,
) http.Handler

func GetHTTPHandlerWithContext added in v0.3.0

func GetHTTPHandlerWithContext(
	lambdaHandler APIGatewayProxyHandlerWithContext,
	resourcePathPattern string,
	stageVariables map[string]string,
	proxyRequestCtx *events.APIGatewayProxyRequestContext,
) http.Handler

Types

type APIGatewayProxyHandler

type APIGatewayProxyHandler func(r events.APIGatewayProxyRequest) (any, error)

type APIGatewayProxyHandlerWithContext

type APIGatewayProxyHandlerWithContext func(ctx context.Context, r events.APIGatewayProxyRequest) (any, error)

Jump to

Keyboard shortcuts

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