chop

package module
v2.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2021 License: MIT Imports: 10 Imported by: 0

README

Chop

Build Status codecov Go Report Card

Chop provides a wrapper to use Go HTTP handlers to handle AWS Lambda events.

This repository started life before native Go Lambda support. Since then AWS have built their own wrapper for Lambda events, available here. Any code going to production should use the officially supported proxy. The primary purpose of this repository is to satisfy my personal interest in API Gateway developments.

Getting started

go get github.com/stevecallear/chop/v2
import (
    "fmt"
    "net/http"

    "github.com/stevecallear/chop"
)

func main() {
    h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "%s %s", r.Method, r.URL.String())
    })

    chop.Start(h)
}

Request Context

Both the Lambda request event and Lambda context are available on the request.

Request Event

Chop will resolve the request event at runtime so the type cannot be guaranteed. The following example demonstrates how to retrieve the event from the request.

h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    switch e := chop.GetEvent(r).(type) {
    case *events.APIGatewayProxyRequest:
        // handle the API gateway proxy integration event
    case *events.APIGatewayV2HTTPRequest:
        // handle the API gateway http v2 event
    case *events.ALBTargetGroupRequest:
        // handle the ALB target group event
    default:
        panic("invalid event")
    }
})

Note: the panic in the example above is unreachable code. Chop will return ErrUnsupportedEventType if the event type cannot be successfully parsed.

Lambda Context

The following example demonstrates how to retrieve the Lambda context from the request.

h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    ctx, ok := lambacontext.FromContext(r.Context())
    // handle the context
})

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnsupportedEventType indicates that the received lambda event is not supported
	ErrUnsupportedEventType = errors.New("unsupported lambda event type")
)

Functions

func GetEvent

func GetEvent(r *http.Request) interface{}

GetEvent returns the lambda event stored within the specified request context if it exists

func Start

func Start(h http.Handler)

Start wraps and starts the specified HTTP handler as a lambda function handler

func WithEvent

func WithEvent(r *http.Request, event interface{}) *http.Request

WithEvent returns a copy of the request with the specified event stored in the request context

Types

type Handler

type Handler struct {
	http.Handler
}

Handler represents a lambda event handler

func Wrap

func Wrap(h http.Handler) *Handler

Wrap wraps the specified HTTP handler as a lambda function handler

func (*Handler) Invoke

func (h *Handler) Invoke(ctx context.Context, payload []byte) ([]byte, error)

Invoke invokes the lambda function handler

type ResponseWriter

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

ResponseWriter represents a lambda event response writer

func NewResponseWriter

func NewResponseWriter() *ResponseWriter

NewResponseWriter returns a new ResponseWriter

func (*ResponseWriter) Body

func (w *ResponseWriter) Body() string

Body returns the response body as a string

func (*ResponseWriter) Header

func (w *ResponseWriter) Header() http.Header

Header returns the response headers

func (*ResponseWriter) Status

func (w *ResponseWriter) Status() string

Status returns the HTTP status as a string

func (*ResponseWriter) StatusCode

func (w *ResponseWriter) StatusCode() int

StatusCode returns the HTTP status code

func (*ResponseWriter) Write

func (w *ResponseWriter) Write(b []byte) (int, error)

func (*ResponseWriter) WriteHeader

func (w *ResponseWriter) WriteHeader(code int)

WriteHeader writes the specified status if the header has not been written

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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