# README
Package gateway provides a drop-in replacement for net/http's ListenAndServe
for use in AWS Lambda & Lambda Function URLs, simply swap it out for gateway.ListenAndServe
.
This project is a fork of apex/Gateway which provides the same functionality for API Gateway and HTTP APIs.
Installation
go get github.com/ericdaugherty/gateway
Example
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/ericdaugherty/gateway"
)
func main() {
http.HandleFunc("/", hello)
log.Fatal(gateway.ListenAndServe(":3000", nil))
}
func hello(w http.ResponseWriter, r *http.Request) {
// example retrieving values from the api gateway proxy request context.
requestContext, ok := gateway.RequestContext(r.Context())
if !ok || requestContext.Authorizer["sub"] == nil {
fmt.Fprint(w, "Hello World from Go")
return
}
userID := requestContext.Authorizer["sub"].(string)
fmt.Fprintf(w, "Hello %s from Go", userID)
}
# Functions
ListenAndServe is a drop-in replacement for http.ListenAndServe for use within AWS Lambda.
NewGateway creates a gateway using the provided http.Handler enabling use in existing aws-lambda-go projects.
NewRequest returns a new http.Request from the given Lambda event.
NewResponse returns a new response writer to capture http output.
RequestContext returns the APIGatewayProxyRequestContext value stored in ctx.
# Structs
Gateway wrap a http handler to enable use as a lambda.Handler.
ResponseWriter implements the http.ResponseWriter interface in order to support the API Gateway Lambda HTTP "protocol".