Categorygithub.com/stevecallear/chop/v2
modulepackage
2.2.1
Repository: https://github.com/stevecallear/chop.git
Documentation: pkg.go.dev

# 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
})

# Packages

No description provided by the author

# Functions

GetEvent returns the lambda event stored within the specified request context if it exists.
NewResponseWriter returns a new ResponseWriter.
Start wraps and starts the specified HTTP handler as a lambda function handler.
WithEvent returns a copy of the request with the specified event stored in the request context.
Wrap wraps the specified HTTP handler as a lambda function handler.

# Variables

ErrUnsupportedEventType indicates that the received lambda event is not supported.

# Structs

No description provided by the author
No description provided by the author