Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrUnsupportedEventType indicates that the supplied event payload is not supported ErrUnsupportedEventType = errors.New("unsupported event type") )
Functions ¶
func New ¶
func New(h HandlerFunc) lambda.Handler
New returns a new lambda handler for the specified function
func NewWithConfig ¶
func NewWithConfig(c Config, h HandlerFunc) lambda.Handler
NewWithConfig returns a new lambda handler for the specified function and configuration
func StatusCode ¶
StatusCode returns the status code for the specified error
Types ¶
type Config ¶
type Config struct { Resolver Resolver Middleware MiddlewareFunc OnBind func(Context, interface{}) error OnError func(Context, error) error OnEmptyResponse HandlerFunc }
Config represent handler configuration
type Context ¶
type Context interface { // Context returns the function invocation context Context() context.Context // Request returns the canonical request Request() *Request // Response returns the canonical response Response() *Response // Get returns the stored value with the specified key Get(key string) interface{} // Set stores the specified value in the context Set(key string, v interface{}) // Path returns the path parameter with the specified key // An empty string is returned if no parameter exists. Path(key string) string // Query returns the first query string parameter with the specified key // An empty string is returned if no parameter exists. If all query string values // are required, then the raw values can be accessed using Request().Query[key]. Query(key string) string // Bind unmarshals the request body into the specified value // Currently only JSON request bodies are supported. Bind(v interface{}) error // NoContent writes the specified status code to the response without a body NoContent(code int) error // String writes the specified status code and value to the response String(code int, s string) error // JSON writes the specified status code and value to the response as JSON JSON(code int, v interface{}) error }
Context represents a handler context
type MiddlewareFunc ¶
type MiddlewareFunc func(HandlerFunc) HandlerFunc
MiddlewareFunc represents a middleware function
func Chain ¶
func Chain(m ...MiddlewareFunc) MiddlewareFunc
Chain returns a middleware func that chains the specified funcs
type Processor ¶
type Processor interface { // CanProcess returns true if the processor is valid for the payload CanProcess(payload []byte) bool // UnmarshalRequest unmarshals the specified payload into a canonical request UnmarshalRequest(payload []byte) (*Request, error) // MarshalResponse marshals the canonical response into a response payload MarshalResponse(res *Response) ([]byte, error) }
Processor represents an event processor
var ( // APIGatewayProxyEventProcessor is an api gateway proxy event processor APIGatewayProxyEventProcessor Processor = &processor{ canProcess: func(payload []byte) bool { pv := gjson.GetManyBytes(payload, "version", "requestContext.apiId") return !pv[0].Exists() && pv[1].Exists() }, unmarshalRequest: func(payload []byte) (*Request, error) { e := new(events.APIGatewayProxyRequest) if err := json.Unmarshal(payload, e); err != nil { return nil, err } q := url.Values(e.MultiValueQueryStringParameters) h := http.Header(e.MultiValueHeaders) return &Request{ Method: e.HTTPMethod, RawPath: e.Path, Path: e.PathParameters, Query: q, Header: h, Body: e.Body, Event: e, }, nil }, marshalResponse: func(r *Response) ([]byte, error) { return json.Marshal(&events.APIGatewayProxyResponse{ StatusCode: r.StatusCode, Headers: reduceHeaders(r.Headers), MultiValueHeaders: r.Headers, Body: r.Body, IsBase64Encoded: false, }) }, } // APIGatewayV2HTTPEventProcessor is an api gateway v2 http event processor APIGatewayV2HTTPEventProcessor Processor = &processor{ canProcess: func(payload []byte) bool { pv := gjson.GetManyBytes(payload, "version", "requestContext.apiId") return pv[0].String() == "2.0" && pv[1].Exists() }, unmarshalRequest: func(payload []byte) (*Request, error) { e := new(events.APIGatewayV2HTTPRequest) if err := json.Unmarshal(payload, e); err != nil { return nil, err } q := url.Values{} for k, ps := range e.QueryStringParameters { for _, v := range strings.Split(ps, ",") { q.Add(k, v) } } h := http.Header{} mergeMaps(e.Headers, nil, h.Add) return &Request{ Method: e.RequestContext.HTTP.Method, RawPath: e.RequestContext.HTTP.Path, Path: e.PathParameters, Query: q, Header: h, Body: e.Body, Event: e, }, nil }, marshalResponse: func(r *Response) ([]byte, error) { return json.Marshal(&events.APIGatewayV2HTTPResponse{ StatusCode: r.StatusCode, Headers: reduceHeaders(r.Headers), MultiValueHeaders: r.Headers, Body: r.Body, IsBase64Encoded: false, Cookies: []string{}, }) }, } // ALBTargetGroupEventProcessor is an alb target group event processor ALBTargetGroupEventProcessor Processor = &processor{ canProcess: func(payload []byte) bool { return gjson.GetBytes(payload, "requestContext.elb").Exists() }, unmarshalRequest: func(payload []byte) (*Request, error) { e := new(events.ALBTargetGroupRequest) if err := json.Unmarshal(payload, e); err != nil { return nil, err } q := url.Values{} mergeMaps(e.QueryStringParameters, e.MultiValueQueryStringParameters, q.Add) h := http.Header{} mergeMaps(e.Headers, e.MultiValueHeaders, h.Add) return &Request{ Method: e.HTTPMethod, RawPath: e.Path, Path: map[string]string{}, Query: q, Header: h, Body: e.Body, Event: e, }, nil }, marshalResponse: func(r *Response) ([]byte, error) { return json.Marshal(&events.ALBTargetGroupResponse{ StatusCode: r.StatusCode, StatusDescription: http.StatusText(r.StatusCode), Headers: reduceHeaders(r.Headers), MultiValueHeaders: r.Headers, Body: r.Body, IsBase64Encoded: false, }) }, } )
type Request ¶
type Request struct { Method string RawPath string Path map[string]string Query url.Values Header http.Header Body string Event interface{} }
Request represents a canonical request type
type Resolver ¶
Resolver represents an event processor resolver
func ResolveConditional ¶
ResolveConditional returns a new conditional event processor resolver The first applicable processor will be returned, based on the incoming payload.
func ResolveStatic ¶
ResolveStatic returns a new static event processor resolver The supplied processor will be invoked for marshal/unmarshal operations, regardless of the incoming payload.
type StatusError ¶
type StatusError struct {
// contains filtered or unexported fields
}
StatusError represents a status code error
func WrapError ¶
func WrapError(code int, err error) *StatusError
WrapError wraps the specified error