api_gateway_proxy

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2020 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	Context  context.Context
	Request  events.APIGatewayProxyRequest
	Response events.APIGatewayProxyResponse
}

func (*Context) Bind

func (c *Context) Bind(v interface{}) error

func (*Context) JSON

func (c *Context) JSON(statusCode int, body interface{}) error

func (*Context) String added in v0.1.0

func (c *Context) String(statusCode int, str string) error

type Error

type Error struct {
	StatusCode int    `json:"-"`
	Message    string `json:"message"`
}

func (Error) Error

func (err Error) Error() string

type HandlerFunc

type HandlerFunc func(c *Context) error

func (HandlerFunc) Middleware

func (hf HandlerFunc) Middleware(middleware ...Middleware) HandlerFunc

Apply middleware to the handler func.

Middleware is called in the order it is given to this function.

func (HandlerFunc) Start

func (hf HandlerFunc) Start()

func (HandlerFunc) ToHttpHandler

func (hf HandlerFunc) ToHttpHandler(
	resourcePathPattern string,
	stageVariables map[string]string,
) http.Handler

ToHTTPHandler turns the Lambdah handler function into a go http.Handler. This is useful for using go http testing tools with API gateway proxy handler.

resourcePathPattern is a path pattern used to simulate AWS API Gateway path matching functionality for use within your tests. E.g. `/books/{bookID}` would be available from the context as c.Request.PathParameters["bookID"]

stageVariables are to simulate any stage variables you have set up on your api gateway in aws

func (HandlerFunc) ToLambdaHandler

func (hf HandlerFunc) ToLambdaHandler() func(
	ctx context.Context,
	request events.APIGatewayProxyRequest,
) (events.APIGatewayProxyResponse, error)

Get the AWS Lambda handler of the handler func.

Useful if you need to call AWS lambda.Start(...) directly, not required in most cases.

type Middleware

type Middleware func(h HandlerFunc) HandlerFunc

func CorrelationIDMiddleware

func CorrelationIDMiddleware() Middleware

Middleware to get or attach a correlation ID to the request, useful for tracing requests through distributed systems. The correlation ID is also returned in a response header so it can be used by consumers. You can also access the correlation ID directly in your handlers and middlewares by calling log.CorrelationIDFromContext(c.Context)

First looks for a Correlation ID provided in the request header `Correlation-Id`. If not present a new correlation ID will be created and put into the request header.

To ensure logs have correlation ID field, this middleware should be called first.

func ErrorHandlerMiddleware

func ErrorHandlerMiddleware() Middleware

Middleware to handle errors returned by handler

If returned error is of type Error{}, then a custom JSON response is returned in the response, with status Error{}.StatusCode and body `{"message": "Error{}.Message"}`

If the logger middleware is also in use, this middleware will log a message for any errors.

If you wish to build a custom error handler you can use this pattern and create a middleware and an Error type with your own structure.

func LoggerMiddleware

func LoggerMiddleware(w io.Writer, fields map[string]string) Middleware

Middleware to configure a logger in the context.Context, which is then used by other middleware (if available), including the error handler.

The middleware logs on the response of each request, and also will include an error log message if the error handler middleware is also used. In addition to the default log messages, you can access the logger in your handlers/middleware by calling log.LoggerFromContext(c.Context). The logger we use is github.com/rs/zerolog

w io.Writer is the log output, for example os.Stdout field map[string]string is a list of key value fields to include in each log message

To ensure that the response status code is correctly reported, this middleware should be called before the error handler middleware.

Jump to

Keyboard shortcuts

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