mbd

package module
v0.0.0-...-e674af2 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2019 License: MIT Imports: 10 Imported by: 0

README

mbd Build Status Go Report Card Test Coverage Go Docs

MBD is a Go framework for AWS Lambda, currently focused on the API Gateway integration. It wraps the official AWS Lambda framework and provides a simplified handler signature, case-insensitive access to headers, query string parameters, and other types of request metadata. It interoperates well with the ibrt/errors package to return configurable, rich error responses, optionally complete with debug information.

Basic Usage
package main

import (
  "github.com/ibrt/mbd"
)

type echoRequest struct {
  Value string `json:"value"`
}

type echoResponse struct {
  Value string `json:"value"`
} 

func main() {
  mbd.NewFunction(echoRequest{}, echoHandler).Start()
}

func(ctx context.Context, req interface{}) (interface{}, error) {
  echoRequest := req.(*echoRequest)
  return &echoResponse{Value: echoRequest.Value}, nil  
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Checker

type Checker func(ctx context.Context, in *events.APIGatewayProxyRequest, req interface{}) (context.Context, error)

Checker implements a check on a request, usually for authentication or validation. It can optionally also add values to the context.

type Debug

type Debug = bool

Debug describes whether the framework should run in debug mode.

func GetDebug

func GetDebug(ctx context.Context) Debug

GetDebug returns the debug flag stored in context. If missing, it returns false.

type ErrorResponse

type ErrorResponse struct {
	StatusCode    int                   `json:"statusCode"`
	PublicMessage string                `json:"publicMessage"`
	RequestID     string                `json:"requestId"`
	Errors        []*ErrorResponseError `json:"errors,omitempty"` // included only if debug context value is set to true
}

ErrorResponse describes an error response.

type ErrorResponseError

type ErrorResponseError struct {
	Error      string   `json:"error"`
	StackTrace []string `json:"stackTrace"`
}

ErrorResponseError is an entry in the Errors section of ErrorResponse.

type Function

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

Function sets up a Lambda function handler.

func NewFunction

func NewFunction(reqTemplate interface{}, handler Handler) *Function

NewFunction initializes a new Function.

func (*Function) AddCheckers

func (e *Function) AddCheckers(checkers ...Checker) *Function

AddCheckers adds one or more Checker(s) to the Function.

func (*Function) AddProviders

func (e *Function) AddProviders(providers ...Provider) *Function

AddProviders adds one or more Provider(s) to the Function.

func (*Function) Handler

Handler provides a handler function suitable for lambda.Start().

func (*Function) SetDebug

func (e *Function) SetDebug(debug Debug) *Function

SetDebug enables or disables additional debug information. Default is disabled.

func (*Function) SetRequestParser

func (e *Function) SetRequestParser(reqParser RequestParser) *Function

SetRequestParser sets a custom RequestParser. Default is JSON.

func (*Function) Start

func (e *Function) Start()

Start invokes lambda.Start() passing the Function handler as argument.

type Handler

type Handler func(ctx context.Context, req interface{}) (resp interface{}, err error)

Handler implements a Lambda function handler.

type Headers

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

Headers provides access to request headers, as original map or case-insensitive getters.

func GetHeaders

func GetHeaders(ctx context.Context) *Headers

GetHeaders returns the Headers stored in context.

func (Headers) Get

func (m Headers) Get(k string) string

Get returns a single value corresponding to the given key, with case-insensitive matching. If the key is not present, it returns "". If the key has multiple values, it returns the last one.

func (Headers) GetMulti

func (m Headers) GetMulti(k string) []string

Get returns the values corresponding to the given key, with case-insensitive matching. If the key is not present, it returns []string{}. If the key has multiple values, it returns all of them.

func (Headers) Map

func (m Headers) Map() map[string]string

Map returns the original values as single-value map, where the single value is the last occurring multi-value.

func (Headers) MapMulti

func (m Headers) MapMulti() map[string][]string

MapMulti returns the original values as a multi-value map.

type Path

type Path struct {
	Resource string
	Path     string
	Method   string
}

Path provides some metadata about the original request path and method.

func GetPath

func GetPath(ctx context.Context) *Path

GetPath returns the Path stored in context.

type PathParameters

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

PathParameters provides access to path parameters, as original map or case-insensitive getter.

func GetPathParameters

func GetPathParameters(ctx context.Context) *PathParameters

GetPathParameters returns the PathParameters stored in context.

func (PathParameters) Get

func (s PathParameters) Get(k string) string

Get returns the value corresponding to the given key, with case-insensitive matching. If the key is not present, it returns "".

func (PathParameters) Map

func (s PathParameters) Map() map[string]string

Map returns the original values as a map.

type Provider

type Provider func(ctx context.Context) context.Context

Provider is a function that populates the Context with some values.

func RequestProvider

func RequestProvider(k interface{}, f func() interface{}) Provider

RequestProvider returns a Provider that generates a new k/v pair for every requests, obtaining v from f.

func SingletonProvider

func SingletonProvider(k, v interface{}) Provider

SingletonProvider returns a Provider that adds the given k/v pair to the context.

type QueryString

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

QueryString provides access to query string parameters, as original map or case-insensitive getters.

func GetQueryString

func GetQueryString(ctx context.Context) *QueryString

GetQueryString returns the QueryString stored in context.

func (QueryString) Get

func (m QueryString) Get(k string) string

Get returns a single value corresponding to the given key, with case-insensitive matching. If the key is not present, it returns "". If the key has multiple values, it returns the last one.

func (QueryString) GetMulti

func (m QueryString) GetMulti(k string) []string

Get returns the values corresponding to the given key, with case-insensitive matching. If the key is not present, it returns []string{}. If the key has multiple values, it returns all of them.

func (QueryString) Map

func (m QueryString) Map() map[string]string

Map returns the original values as single-value map, where the single value is the last occurring multi-value.

func (QueryString) MapMulti

func (m QueryString) MapMulti() map[string][]string

MapMulti returns the original values as a multi-value map.

type RequestContext

type RequestContext = events.APIGatewayProxyRequestContext

RequestContext is an alias for events.APIGatewayProxyRequestContext.

func GetRequestContext

func GetRequestContext(ctx context.Context) *RequestContext

GetRequestContext returns the RequestContext stored in context.

type RequestParser

type RequestParser func(context.Context, reflect.Type, *events.APIGatewayProxyRequest) (interface{}, error)

RequestParser describes a custom request parser. The default is JSON.

func FormRequestParser

func FormRequestParser() RequestParser

FormRequestParser returns a RequestParser for form encoded requests. It uses gorilla/schema to map values to a struct.

func JSONRequestParser

func JSONRequestParser() RequestParser

JSONRequestParser returns a RequestParser for JSON requests.

type SerializedResponse

type SerializedResponse struct {
	ContentType     string
	IsBase64Encoded bool
	Body            string
}

SerializedResponse allows returning a non-JSON response body, passed through as it is.

type StageVariables

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

StageVariables provides access to stage variables, as original map or case-insensitive getter.

func GetStageVariables

func GetStageVariables(ctx context.Context) *StageVariables

GetStageVariables returns the GetStageVariables stored in context.

func (StageVariables) Get

func (s StageVariables) Get(k string) string

Get returns the value corresponding to the given key, with case-insensitive matching. If the key is not present, it returns "".

func (StageVariables) Map

func (s StageVariables) Map() map[string]string

Map returns the original values as a map.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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