apigatewayrouter

package module
v0.0.0-...-43e40f2 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2020 License: MIT Imports: 3 Imported by: 0

README

AWS APIGateway Lambda Router

Library for routing APIGateway requests in a Lambda function.

Build Status Coverage Status Go Report Card

Docs

https://godoc.org/github.com/markwilson/apigatewayrouter

Example usage

package main

import (
	"log"
	"net/http"
	"regexp"

	"github.com/aws/aws-lambda-go/events"
	"github.com/aws/aws-lambda-go/lambda"
	"github.com/markwilson/apigatewayrouter"
)

func main() {
	r := apigatewayrouter.NewRouter()

	r.AddRoute("health", &apigatewayrouter.Route{
		Match: func(req events.APIGatewayProxyRequest) bool {
			return req.Path == "/check/health1"
		},
		Handle: func(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
			log.Println(r.CurrentRouteName)

			return events.APIGatewayProxyResponse{
				Body:       "OK",
				StatusCode: 200,
			}, nil
		},
	})

	r.AddStaticRoute("health2", http.MethodGet, "/check/health2", func(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
		log.Println(r.CurrentRouteName)

		return events.APIGatewayProxyResponse{
			Body:       "OK",
			StatusCode: 200,
		}, nil
	})

	r.AddRegExpRoute("health3", http.MethodGet, regexp.MustCompile("^/check/health3$"), func(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
		log.Println(r.CurrentRouteName)

		return events.APIGatewayProxyResponse{
			Body:       "OK",
			StatusCode: 200,
		}, nil
	})
	
	lambda.Start(r.Handle)
}

Documentation

Overview

Package apigatewayrouter is a basic router for APIGateway-triggered Lambda functions. Each route defined in the router needs to be replicated into the APIGateway configuration.

Example
// Initialise a new Router.
r := NewRouter()

// Add a single Route to it.
r.AddRoute("test", &Route{
	Match: func(req events.APIGatewayProxyRequest) bool {
		return req.Path == "/test"
	},
	Handle: func(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
		fmt.Println("Current route:", r.CurrentRouteName)

		return events.APIGatewayProxyResponse{
			Body:       "OK",
			StatusCode: 200,
		}, nil
	},
})

// Start handling request events.
lambda.Start(r.Handle)

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HandleFunc

HandleFunc handles a matched APIGateway request event.

type MatchFunc

type MatchFunc func(events.APIGatewayProxyRequest) bool

MatchFunc checks if the APIGateway request event can be handled.

type Route

type Route struct {
	Match  MatchFunc
	Handle HandleFunc
}

Route determines if it can handle an APIGateway request event and handles it if possible.

type Router

type Router struct {
	CurrentRouteName string
	Routes           map[string]*Route
	NotFound         HandleFunc
}

Router stores a map of named routes which will be looped over to find the first matching Route.

func NewRouter

func NewRouter() *Router

NewRouter creates a new empty Router.

func (*Router) AddRegExpRoute

func (r *Router) AddRegExpRoute(name string, method string, re *regexp.Regexp, handler HandleFunc) *Router

AddRegExpRoute creates a MatchFunc using a regular expression matcher then adds it and the handler to the Router using AddRoute.

func (*Router) AddRoute

func (r *Router) AddRoute(name string, route *Route) *Router

AddRoute puts the defined Route into the Router. There is no clash detection for route names, if the same name string is used multiple times then only the most recent Route value is used.

This is also used internally by the other `Add*Route` functions.

func (*Router) AddStaticRoute

func (r *Router) AddStaticRoute(name string, method string, uri string, handler HandleFunc) *Router

AddStaticRoute creates a MatchFunc for an exact path match then adds it and the handler to the Router using AddRoute.

func (*Router) Handle

Handle is the routing part of the Router, it is responsible for finding a matching Route and executing it. If no matching routes are found, an error is triggered.

This function is a valid handler for use in lambda.Start - for example:-

r := NewRouter()
// configure the router's routes...
lambda.Start(r.Handle)

Jump to

Keyboard shortcuts

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