apigatewayproxy

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2018 License: MIT Imports: 11 Imported by: 0

README

apigatewayproxy GoDoc License Build Status Coverage Status GoReportCard

Package apigatwayproxy provides a way to process AWS API Gateway Proxy requests using a standard http.Handler.

This makes it simple to build a program that operates as a HTTP server when running outside an AWS Lambda container, and runs as an AWS Lambda when running inside an AWS Lambda container.

Read the package documentation for more information.

Licence

MIT

Documentation

Overview

Package apigatewayproxy provides a way to process AWS API Gateway Proxy requests using a standard HTTP handler. This makes it simple to build a program that operates as a HTTP server when run normally, and runs as an AWS lambda when running in an AWS lambda container.

Example
package main

import (
	"net/http"

	"github.com/jjeffery/apigatewayproxy"
)

func main() {
	// define a simple HTTP handler
	h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("hello world\n"))
	})

	if apigatewayproxy.IsLambda() {
		// this process is running in an AWS Lambda container
		apigatewayproxy.Start(h)
	} else {
		// run as a conventional HTTP server
		http.ListenAndServe(":8080", h)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// RequestReceived is called when a request is received from Lambda. Useful for logging.
	// The default implementation does nothing.
	RequestReceived func(request *events.APIGatewayProxyRequest)

	// SendingResponse is called just prior to returning the response to Lambda. Useful for logging.
	// The default implementation does nothing.
	SendingResponse func(request *events.APIGatewayProxyRequest, response *events.APIGatewayProxyResponse)

	// ShouldEncodeBody is called to determine if the body should be base64-encoded.
	// The default implementation returns true if the response has a Content-Encoding header,
	// or if body contains bytes outside the range [0x09, 0x7f].
	ShouldEncodeBody func(response *events.APIGatewayProxyResponse, body []byte) bool
)

Callback functions that can be overridden.

Functions

func IsLambda

func IsLambda() bool

IsLambda returns true if the current process is operating in an AWS Lambda container. It determines this by checking for the presence of the "_LAMBDA_SERVER_PORT" environment variable.

func Request

Request returns a pointer to the API Gateway proxy request, or nil if the current context is not associated with an API Gateway proxy lambda.

Example
package main

import (
	"fmt"
	"net/http"

	"github.com/jjeffery/apigatewayproxy"
)

func main() {
	// define a HTTP handler that knows whether it is
	// running as an AWS lambda
	h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		pr := apigatewayproxy.Request(r.Context())
		var msg string
		if pr == nil {
			msg = "I am not running in an AWS Lambda container"
		} else {
			msg = fmt.Sprintf("I am running in an AWS Lambda under account %s", pr.RequestContext.AccountID)
		}
		w.Write([]byte(msg))
	})

	if apigatewayproxy.IsLambda() {
		// this process is running in an AWS Lambda container
		apigatewayproxy.Start(h)
	} else {
		// run as a conventional HTTP server
		http.ListenAndServe(":8080", h)
	}
}
Output:

func Start

func Start(h http.Handler)

Start starts handling AWS Lambda API Gateway proxy requests by passing each request to the HTTP hander function.

Types

This section is empty.

Jump to

Keyboard shortcuts

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