chop

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2019 License: MIT Imports: 7 Imported by: 0

README

Chop

Build Status codecov Go Report Card

Chop provides a wrapper to use Go HTTP handlers to handle AWS Lambda API Gateway proxy integration events.

Getting started

go get github.com/stevecallear/chop
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)
}

Deploying

Follow the 'Build and deploy' steps in this AWS blog post.

Request Context

Both the proxy integration event and lambda context are stored in the request context. They can be accessed using the chop.GetEvent and chop.GetContext functions respectively.

func main() {
    h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        e, _ := chop.GetEvent(r)
        fmt.Fprintf(w, "Stage: %s", e.RequestContext.Stage)
        
        c, _ := chop.GetContext(r)
        fmt.Fprintf(w, "AwsRequestID: %s", c.AwsRequestID)
    })

    chop.Start(h)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetContext

func GetContext(r *http.Request) (lambdacontext.LambdaContext, bool)

GetContext returns a copy of the lambda context if it exists

func GetEvent

GetEvent returns a copy of the proxy integration event if it exists

func NewRequest

NewRequest parses the integration event and returns a new HTTP request

func Start

func Start(h http.Handler)

Start wraps and starts specified HTTP handler as a proxy integration event handler

func WithEvent

WithEvent returns a copy of the request with the specified event stored in the request context The function is exported to simplify testing for apps that use GetEvent

Types

type Handler

type Handler struct {
	http.Handler
}

Handler represents a proxy integration event handler

func Wrap

func Wrap(h http.Handler) *Handler

Wrap wraps the specified HTTP handler with a proxy integration event handler

func (*Handler) Handle

Handle dispatches the integration event as an HTTP request to the wrapped handler

type ResponseWriter

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

ResponseWriter represents a proxy integration response writer

func NewResponseWriter

func NewResponseWriter() *ResponseWriter

NewResponseWriter returns a new ResponseWriter

func (*ResponseWriter) Header

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

Header returns the HTTP headers

func (*ResponseWriter) Result

Result returns a proxy integration result for the response

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