algnhsa

package module
v0.0.0-...-b9ba6e5 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2019 License: Apache-2.0 Imports: 10 Imported by: 1

README

algnhsa GoDoc Build Status

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

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

package main

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

    "github.com/akrylysov/algnhsa"
)

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 main() {
    http.HandleFunc("/", indexHandler)
    http.HandleFunc("/add", addHandler)
    algnhsa.ListenAndServe(http.DefaultServeMux, nil)
}

Plug in a third-party HTTP router:

package main

import (
    "net/http"

    "github.com/akrylysov/algnhsa"
    "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)
}

More details at http://artem.krylysov.com/blog/2018/01/18/porting-go-web-applications-to-aws-lambda/.

Documentation

Overview

Example
package main

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

	"github.com/akrylysov/algnhsa"
)

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)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListenAndServe

func ListenAndServe(handler http.Handler, binaryContentTypes []string)

ListenAndServe starts the AWS Lambda runtime (aws-lambda-go lambda.Start) with a given handler. It accepts a slice of content types that should be treated as binary types by the API Gateway. The "*/* value makes algnhsa treat any content type as binary.

func ProxyRequestFromContext

func ProxyRequestFromContext(ctx context.Context) (events.APIGatewayProxyRequest, bool)

ProxyRequestFromContext extracts the APIGatewayProxyRequest event from ctx.

Types

This section is empty.

Jump to

Keyboard shortcuts

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