proxy

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2022 License: MIT Imports: 9 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CatchAllHandler

CatchAllHandler defines the function interface the router uses to handle any request that doesn't match a route.

type ErrorHandler

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:

  1. Form POSTs
  2. Route defined regex capture
  3. Query string
  4. AWS API Gateway configured PathParameters.

func (*Route) Follow

Follow extracts the route context for the given request and executed the route's handler function.

func (*Route) IsMatch

func (route *Route) IsMatch(request events.APIGatewayV2HTTPRequest) (bool, []string)

IsMatch return true if there is a match otherwise false. The match groups are also returned.

func (*Route) String

func (route *Route) String() string

String returns a string representation of this route.

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

func (router *Router) AddBuildError(err error)

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) AddRoute

func (router *Router) AddRoute(route *Route)

AddRoute appends route to the list of routes used for request matching.

func (*Router) AddRouteIfNoError

func (router *Router) AddRouteIfNoError(route *Route, err error)

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

func (router *Router) BuildErrors() error

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

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.

func (*Router) Valid

func (router *Router) Valid() bool

Valid returns true if the routers' routes have all been built successfully. Otherwise false.

Jump to

Keyboard shortcuts

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