lrtr

package
v1.3.7 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package lrtr is a simple-to-use library for writing AWS Lambda functions in Go that listen to events of type API Gateway Proxy Req (represented by the `events.APIGatewayProxyRequest` type of the github.com/aws-lambda-go/events package).

The library allows creating functions that can match reqs based on their URI, just like an HTTP server that uses the standard https://golang.org/pkg/net/http/#ServeMux (or any other community-built routing library such as https://github.com/julienschmidt/httprouter or https://github.com/go-chi/chi) would. The interface provided by the library is very similar to these libraries and should be familiar to anyone who has written HTTP applications in Go.

Use Case

When building large cloud-native applications, there's a certain balance to strike when it comes to deployment of APIs. On one side of the scale, each API endpoint has its own lambda function. This provides the greatest flexibility, but is extremely difficult to maintain. On the other side of the scale, there can be one lambda function for the entire API. This provides the least flexibility, but is the easiest to maintain. Both are probably not a good idea.

With `lmdrouter`, one can create small lambda functions for different aspects of the API. For example, if your application model contains multiple domains (e.g. articles, authors, topics, etc...), you can create one lambda function for each domain, and deploy these independently (e.g. everything below "/api/articles" is one lambda function, everything below "/api/authors" is another function). This is also useful for applications where different teams are in charge of different parts of the API.

Features

* Supports all HTTP methods.

* Supports middleware functions at a global and per-resource level.

* Supports path parameters with a simple ":<name>" format (e.g. "/posts/:id").

* Provides ability to automatically "unmarshal" an API Gateway req to an arbitrary Go struct, with data coming either from path and query string parameters, or from the req body (only JSON reqs are currently supported). See the documentation for the `UnmarshalReq` function for more information.

* Provides the ability to automatically "marshal" responses of any type to an API Gateway response (only JSON responses are currently generated). See the Custom function for more information.

  • Implements net/http.Handler for local development and general usage outside an AWS Lambda environment.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Router

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

Router is the main type of the library. Lambda routes are registered to it, and it's Handler method is used by the lambda to match reqs and execute the appropriate handler.

func NewRouter

func NewRouter(basePath string, middleware ...lcom.Middleware) (l *Router)

NewRouter creates a new Router object with a base path and a list of zero or more global middleware functions. The base path is necessary if the lambda function is not going to be mounted to a domain's root (for example, if the function is mounted to "https://my.app/api", then the base path must be "/api"). Use an empty string if the function is mounted to the root of the domain.

func (*Router) Handler

Handler receives a context and an API Gateway Proxy req, and handles the req, matching the appropriate handler and executing it. This is the method that must be provided to the lambda's `main` function:

package main

import (
    "github.com/aws/aws-lambda-go/lambda"
    "github.com/seantcanavan/lmdrouter"
)

var router *lmdrouter.Router

func init() {
    router = lmdrouter.NewRouter("/api", loggerMiddleware, authMiddleware)
    router.Route(http.MethodGet, "/", listSomethings)
    router.Route(http.MethodPost, "/", postSomething, someOtherMiddleware)
    router.Route(http.MethodGet, "/:id", getSomething)
    router.Route("PUT", "/:id", updateSomething)
    router.Route(http.MethodDelete, "/:id", deleteSomething)
}

func main() {
    lambda.Start(router.Handler)
}

func (*Router) Route

func (l *Router) Route(method, path string, handler lcom.Handler, middleware ...lcom.Middleware)

Route registers a new route, with the provided HTTP method name and path, and zero or more local middleware functions.

func (*Router) ServeHTTP

func (l *Router) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServerHTTP implements the net/http.Handler interface in order to allow lmdrouter applications to be used outside of AWS Lambda environments, most likely for local development purposes

Jump to

Keyboard shortcuts

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