All Projects → aquasecurity → lmdrouter

aquasecurity / lmdrouter

Licence: Apache-2.0 license
Go HTTP router library for AWS API Gateway-invoked Lambda Functions

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to lmdrouter

getting-started-with-serverless
Follow along with blog posts, code samples, and practical exercises to learn how to build serverless applications from your local Integrated development environment (IDE).
Stars: ✭ 46 (-61.98%)
Mutual labels:  api-gateway, lambda-functions
yap
Open source API Gateway on GraphQL for serverless. Less code, safer API. 💪
Stars: ✭ 105 (-13.22%)
Mutual labels:  api-gateway, lambda-functions
Serverless Aws Alias
Alias support for Serverless 1.x
Stars: ✭ 171 (+41.32%)
Mutual labels:  api-gateway, lambda-functions
pong
🏓 Pong for RESTful APIs (microservices pattern) using Serverless Framework ⚡
Stars: ✭ 27 (-77.69%)
Mutual labels:  api-gateway, lambda-functions
aws-tailor
AWS account provisioning and management service
Stars: ✭ 105 (-13.22%)
Mutual labels:  api-gateway, lambda-functions
Lambda Proxy Router
A simple router for AWS Lambda Proxy Functions
Stars: ✭ 14 (-88.43%)
Mutual labels:  api-gateway, lambda-functions
CloudFrontier
Monitor the internet attack surface of various public cloud environments. Currently supports AWS, GCP, Azure, DigitalOcean and Oracle Cloud.
Stars: ✭ 102 (-15.7%)
Mutual labels:  api-gateway, lambda-functions
sls-photos-upload-service
Example web app and serverless API for uploading photos and saving to S3 and DynamoDB
Stars: ✭ 50 (-58.68%)
Mutual labels:  api-gateway
apex-api-gateway-boilerplate
Boilerplate for AWS Lambda and API Gateway using Apex
Stars: ✭ 19 (-84.3%)
Mutual labels:  api-gateway
go2gql
graphql-go schema generator by proto files
Stars: ✭ 33 (-72.73%)
Mutual labels:  api-gateway
functions
An Open Source Serverless Platform
Stars: ✭ 44 (-63.64%)
Mutual labels:  lambda-functions
gobis
Gobis is a lightweight API Gateway written in go which can be used programmatically or as a standalone server.
Stars: ✭ 48 (-60.33%)
Mutual labels:  api-gateway
parser
arc.app, .arc, arc.json, arc.yaml, and arc.toml support
Stars: ✭ 20 (-83.47%)
Mutual labels:  api-gateway
aws-batch-example
Example use of AWS batch
Stars: ✭ 96 (-20.66%)
Mutual labels:  lambda-functions
kong-plugin-api-response-merger
Kong API response merger plugin
Stars: ✭ 14 (-88.43%)
Mutual labels:  api-gateway
GatewayService
GatewayService (Ocelot).
Stars: ✭ 19 (-84.3%)
Mutual labels:  api-gateway
spacesuit
API Gateway with URL remapping
Stars: ✭ 19 (-84.3%)
Mutual labels:  api-gateway
data
[deprecated] Generate a DynamoDB data access layer from an .arc file. Automatically disambiguates testing (in memory) from deployment staging and production tables
Stars: ✭ 20 (-83.47%)
Mutual labels:  api-gateway
noiiice
a serverless blog built on NuxtJS, AWS, serverless framework, and irrational exuberance.
Stars: ✭ 42 (-65.29%)
Mutual labels:  api-gateway
serverless-websockets-chat
Realtime chat app based on AWS Lambda, API Gateway, DynamoDB, Websockets, React in TS
Stars: ✭ 19 (-84.3%)
Mutual labels:  api-gateway

lmdrouter

Build Status

Go HTTP router library for AWS API Gateway-invoked Lambda Functions

Table of Contents

Overview

lmdrouter is a simple-to-use library for writing AWS Lambda functions in Go that listen to events of type API Gateway Proxy Request. It allows creating a lambda function that can match requests based on their URI, just like an HTTP server would.

The library provides an interface not unlike the standard net/http.Mux type or community libraries such as httprouter and chi.

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 at a global and per-resource level.
  • Supports path parameters with a simple ":" format (e.g. "/posts/:id").
  • Provides ability to automatically "unmarshal" an API Gateway request to an arbitrary Go struct, with data coming from the request path, the query string, the headers and the request body (only JSON requests are currently supported).
  • Provides ability to automatically "marshal" responses of any type to an API Gateway response (only JSON responses are currently generated).
  • Implements net/http.Handler for running locally or as a simple HTTP server.

Installation

go get github.com/aquasecurity/lmdrouter

Usage

lmdrouter is meant to be used inside Go Lambda functions.

package main

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

var router *lmdrouter.Router

func init() {
    router = lmdrouter.NewRouter("/api", loggerMiddleware, authMiddleware)
    router.Route("GET", "/", listSomethings)
    router.Route("POST", "/", postSomething, someOtherMiddleware)
    router.Route("GET", "/:id", getSomething)
    router.Route("PUT", "/:id", updateSomething)
    router.Route("DELETE", "/:id", deleteSomething)
}

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

// the rest of the code is a redacted example, it will probably reside in a
// separate package inside your project

type listSomethingsInput struct {
    ID                string   `lambda:"path.id"`                // a path parameter declared as :id
    ShowSomething     bool     `lambda:"query.show_something"`   // a query parameter named "show_something"
    AcceptedLanguages []string `lambda:"header.Accept-Language"` // a multi-value header parameter
}

type postSomethingInput struct {
    Title   string    `json:"title"`
    Date    time.Time `json:"date"`
}

func listSomethings(ctx context.Context, req events.APIGatewayProxyRequest) (
    res events.APIGatewayProxyResponse,
    err error,
) {
    // parse input from request and path parameters
    var input listSomethingsInput
    err = lmdrouter.UnmarshalRequest(req, false, &input)
    if err != nil {
        return lmdrouter.HandleError(err)
    }

    // call some business logic that generates an output struct
    // ...

    return lmdrouter.MarshalResponse(http.StatusOK, nil, output)
}

func postSomethings(ctx context.Context, req events.APIGatewayProxyRequest) (
    res events.APIGatewayProxyResponse,
    err error,
) {
    // parse input from request body
    var input postSomethingsInput
    err = lmdrouter.UnmarshalRequest(req, true, &input)
    if err != nil {
        return lmdrouter.HandleError(err)
    }

    // call some business logic that generates an output struct
    // ...

    return lmdrouter.MarshalResponse(http.StatusCreated, nil, output)
}

func loggerMiddleware(next lmdrouter.Handler) lmdrouter.Handler {
    return func(ctx context.Context, req events.APIGatewayProxyRequest) (
        res events.APIGatewayProxyResponse,
        err error,
    ) {
        // [LEVEL] [METHOD PATH] [CODE] EXTRA
        format := "[%s] [%s %s] [%d] %s"
    	level := "INF"
    	var code int
    	var extra string

    	res, err = next(ctx, req)
    	if err != nil {
    	    level = "ERR"
    	    code = http.StatusInternalServerError
    	    extra = " " + err.Error()
    	} else {
    	    code = res.StatusCode
    	    if code >= 400 {
    	        level = "ERR"
    	    }
        }

        log.Printf(format, level, req.HTTPMethod, req.Path, code, extra)

        return res, err
    }
}

Note that for requests with a body (e.g. POST, PUT, PATCH), the struct types of your inputs can contain parameters from all sources: path parameters, query string parameters, request headers and the JSON body. Anything that doesn't have a lambda struct tag is taken from the body. Regular json tags can be used for that.

Static Compilation for AWS Lambda

To ensure Lambda applications using lmdrouter (or any Lambda applications written in Go, for that matter) will properly work in AWS's Go runtime, make sure to compile your applications statically. You can either disable CGO completely using CGO_ENABLED=0, or use the following build flags:

go build -tags netgo -ldflags "-s -w"

License

This library is distributed under the terms of the Apache License 2.0.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].