Git Product home page Git Product logo

go-api-gateway-lambda-connector's Introduction

algnhsa is an AWS Lambda Go net/http server adapter.

algnhsa enables running Go web applications on AWS Lambda and API Gateway or ALB without changing the existing HTTP handlers:

package main

import (
    "fmt"
    "net/http"
    "strconv"

    "github.com/anthonyLock/go-api-gateway-lambda-connector"
)

func indexHandler(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("index"))
}

func addHandler(w http.ResponseWriter, r *http.Request) {
    f, _ := strconv.Atoi(r.FormValue("first"))
    s, _ := strconv.Atoi(r.FormValue("second"))
    w.Header().Set("X-Hi", "foo")
    fmt.Fprintf(w, "%d", f+s)
}

func contextHandler(w http.ResponseWriter, r *http.Request) {
    proxyReq, ok := algnhsa.ProxyRequestFromContext(r.Context())
    if ok {
        fmt.Fprint(w, proxyReq.RequestContext.AccountID)
    }
}

func main() {
    http.HandleFunc("/", indexHandler)
    http.HandleFunc("/add", addHandler)
    http.HandleFunc("/context", contextHandler)
    algnhsa.ListenAndServe(http.DefaultServeMux, nil)
}

Plug in a third-party HTTP router

package main

import (
    "net/http"

    "github.com/anthonyLock/go-api-gateway-lambda-connector"
    "github.com/go-chi/chi"
)

func main() {
    r := chi.NewRouter()
    r.Get("/", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("index"))
    })
    algnhsa.ListenAndServe(r, nil)
}

Setting up API Gateway

  1. Create a new REST API.

  2. In the "Resources" section create a new ANY method to handle requests to / (check "Use Lambda Proxy Integration").

    API Gateway index

  3. Add a catch-all {proxy+} resource to handle requests to every other path (check "Configure as proxy resource").

    API Gateway catch-all

Setting up ALB

  1. Create a new ALB and point it to your Lambda function.

  2. In the target group settings enable "Multi value headers".

go-api-gateway-lambda-connector's People

Contributors

akrylysov avatar anthonylock avatar rosshammer avatar a-h avatar mspiegel avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.