Documentation ¶
Overview ¶
Package proxy provides utilities for writing aws lambda functions that act as aws api gateway v2 (http) integrations. Specifically they assist in adding routing functionality and processing the entire request/response through the lambda via events.APIGatewayV2HTTPRequest and events.APIGatewayProxyResponse.
The router is designed to be as simplistic as possible and is not feature rich.
Index ¶
- type CatchAllHandler
- type ErrorHandler
- type HttpMethod
- type Route
- func (route *Route) Context(ctx context.Context, request events.APIGatewayV2HTTPRequest, groups []string) (*RouteContext, error)
- func (route *Route) Follow(ctx context.Context, request events.APIGatewayV2HTTPRequest, groups []string) (events.APIGatewayProxyResponse, error)
- func (route *Route) IsMatch(request events.APIGatewayV2HTTPRequest) (bool, []string)
- func (route *Route) String() string
- type RouteContext
- type RouteHandler
- type Router
- func (router *Router) AddBuildError(err error)
- func (router *Router) AddCatchAllHandler(handler CatchAllHandler)
- func (router *Router) AddErrorHandler(handler ErrorHandler)
- func (router *Router) AddRoute(route *Route)
- func (router *Router) AddRouteIfNoError(route *Route, err error)
- func (router *Router) BuildErrors() error
- func (router *Router) CONNECT(match string, handler RouteHandler)
- func (router *Router) DELETE(match string, handler RouteHandler)
- func (router *Router) GET(match string, handler RouteHandler)
- func (router *Router) HEAD(match string, handler RouteHandler)
- func (router *Router) OPTIONS(match string, handler RouteHandler)
- func (router *Router) PATCH(match string, handler RouteHandler)
- func (router *Router) POST(match string, handler RouteHandler)
- func (router *Router) PUT(match string, handler RouteHandler)
- func (router *Router) Route(ctx context.Context, request events.APIGatewayV2HTTPRequest) (events.APIGatewayProxyResponse, error)
- func (router *Router) TRACE(match string, handler RouteHandler)
- func (router *Router) Valid() bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CatchAllHandler ¶
type CatchAllHandler func(context.Context, events.APIGatewayV2HTTPRequest) (events.APIGatewayProxyResponse, error)
CatchAllHandler defines the function interface the router uses to handle any request that doesn't match a route.
type ErrorHandler ¶
type ErrorHandler func(context.Context, events.APIGatewayV2HTTPRequest, error) (events.APIGatewayProxyResponse, error)
ErrorHandler defines the function interface the router uses to handle any error that occurs while processing routes.
type HttpMethod ¶
type HttpMethod int
HttpMethod is an enum of the standard Http Methods.
const ( GET HttpMethod = iota HEAD POST PUT DELETE CONNECT OPTIONS TRACE PATCH )
func (HttpMethod) String ¶
func (i HttpMethod) String() string
type Route ¶
type Route struct { Method HttpMethod Regex *regexp.Regexp Handler RouteHandler }
Route defines a HttpMethod and Regex that are used in combination for matching against an incoming request. When a match occurs the configured handler is called.
func NewRoute ¶
func NewRoute(method HttpMethod, pattern string, handler RouteHandler) (*Route, error)
NewRoute returns a Route for the specified method, pattern and handler.
func (*Route) Context ¶
func (route *Route) Context(ctx context.Context, request events.APIGatewayV2HTTPRequest, groups []string) (*RouteContext, error)
Context constructs a RouteContext for the route for passing to the handler. The 'Params' that get set on the context are extracted from the request with the following precedence:
- Form POSTs
- Route defined regex capture
- Query string
- AWS API Gateway configured PathParameters.
func (*Route) Follow ¶
func (route *Route) Follow(ctx context.Context, request events.APIGatewayV2HTTPRequest, groups []string) (events.APIGatewayProxyResponse, error)
Follow extracts the route context for the given request and executed the route's handler function.
type RouteContext ¶
type RouteContext struct { Context context.Context Request events.APIGatewayV2HTTPRequest Params map[string]string }
RouteContext contains all the request information for a route when matched.
func (*RouteContext) Body ¶ added in v0.3.0
func (ctx *RouteContext) Body() (string, error)
Body returns a string representation of the request body
type RouteHandler ¶
type RouteHandler func(*RouteContext) (events.APIGatewayProxyResponse, error)
RouteHandler defines the function interface the route uses to execute a request when the route is matched.
type Router ¶
type Router struct { Routes []*Route CatchAll CatchAllHandler CatchError ErrorHandler // contains filtered or unexported fields }
Router will route an incoming events.APIGatewayV2HTTPRequest to the appropriate route based upon the router configuration and then return the events.APIGatewayProxyResponse.
Route matching is a simple process that loops through all routes added in the order they were configured and checks if a match is present. If so that route gets executed, otherwise it moves onto the next route for comparison.
If the CatchAll handler is set any request that doesn't match a route will be handled by it.
If the CatchError handler is set any route that returns an error will first be passed into the hander for additional processing.
Example:
func yoloHandler(ctx *RouteContext) (events.APIGatewayProxyResponse, error) { headers := map[string]string{ "Content-Type": "application/json", } response := events.APIGatewayProxyResponse{ StatusCode: 200, Headers: headers, Body: `{"yolo": "it's true"}`, IsBase64Encoded: false, } return response, nil } func handler(ctx context.Context, request events.APIGatewayV2HTTPRequest) (events.APIGatewayProxyResponse, error) { router := &proxy.Router{} router.GET("/yolo", yoloHandler) if !router.Valid() { return events.APIGatewayProxyResponse{}, router.BuildErrors() } return router.Route(ctx, request) }
func (*Router) AddBuildError ¶
AddBuildError appends an error to the list of router errors.
func (*Router) AddCatchAllHandler ¶
func (router *Router) AddCatchAllHandler(handler CatchAllHandler)
AddCatchAllHandler attaches a catchall handler to the router.
func (*Router) AddErrorHandler ¶
func (router *Router) AddErrorHandler(handler ErrorHandler)
AddErrorHandler attaches a error handler to the router.
func (*Router) AddRouteIfNoError ¶
AddRouteIfNoError appends the provided route if no error is present. Otherwise it adds the error to the build errors.
This method is provided to simplify router construction with many routes by reducing error checking boilerplate.
func (*Router) BuildErrors ¶
BuildErrors returns a single error that encapsulates all the route errors found during router construction.
func (*Router) CONNECT ¶
func (router *Router) CONNECT(match string, handler RouteHandler)
CONNECT adds a new CONNECT route with the specified pattern match and handler.
func (*Router) DELETE ¶
func (router *Router) DELETE(match string, handler RouteHandler)
DELETE adds a new DELETE route with the specified pattern match and handler.
func (*Router) GET ¶
func (router *Router) GET(match string, handler RouteHandler)
GET adds a new GET route with the specified pattern match and handler.
func (*Router) HEAD ¶
func (router *Router) HEAD(match string, handler RouteHandler)
HEAD adds a new HEAD route with the specified pattern match and handler.
func (*Router) OPTIONS ¶
func (router *Router) OPTIONS(match string, handler RouteHandler)
OPTIONS adds a new OPTIONS route with the specified pattern match and handler.
func (*Router) PATCH ¶
func (router *Router) PATCH(match string, handler RouteHandler)
PATCH adds a new PATCH route with the specified pattern match and handler.
func (*Router) POST ¶
func (router *Router) POST(match string, handler RouteHandler)
POST adds a new POST route with the specified pattern match and handler.
func (*Router) PUT ¶
func (router *Router) PUT(match string, handler RouteHandler)
PUT adds a new PUT route with the specified pattern match and handler.
func (*Router) Route ¶
func (router *Router) Route(ctx context.Context, request events.APIGatewayV2HTTPRequest) (events.APIGatewayProxyResponse, error)
Route loops through all routes and checks if the request matches any of them.
If there is a match it executes the route's handler.
If the catch all handler is set and no route is matched it gets executed.
If there is no catch all handler and no route is matched an error is returned.
If there is an error handler set and an error occurs the errors the error handler is executed and it's result returned.
func (*Router) TRACE ¶
func (router *Router) TRACE(match string, handler RouteHandler)
TRACE adds a new TRACE route with the specified pattern match and handler.