gateway

package module
v1.22.0 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2022 License: MIT Imports: 11 Imported by: 0

README

GoDoc Calver v0.YY.Minor

Package gateway provides a drop-in replacement for net/http's ListenAndServe for use in AWS Lambda & API Gateway, simply swap it out for gateway.ListenAndServe. Extracted from Up which provides additional middleware features and operational functionality.

This version is forked from Apex/gateway, which tended to merge pull requests very infrequently. Another fork, by piotrkubisa, was good, but he has discontinued work.

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/carlmjohnson/gateway"
)

func main() {
	http.HandleFunc("/", hello)
	log.Fatal(gateway.ListenAndServe("n/a", 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)
}

Documentation

Overview

Package gateway provides a drop-in replacement for net/http.ListenAndServe for use in AWS Lambda & API Gateway.

Example
package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/carlmjohnson/gateway"
)

func main() {
	http.HandleFunc("/", hello)
	log.Fatal(gateway.ListenAndServe(":3000", nil))
}

func hello(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "Hello World from Go")
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListenAndServe

func ListenAndServe(host string, h http.Handler) error

ListenAndServe is a drop-in replacement for http.ListenAndServe for use within AWS Lambda. Because the standard addr string is not used, it is replaced with host, which API Gateway does not always send with events.

ListenAndServe never returns.

func NewRequest

NewRequest returns a new http.Request from the given Lambda event.

func RequestContext

RequestContext returns the APIGatewayProxyRequestContext value stored in ctx.

Types

type Handler added in v1.21.0

Handler is the API Gateway Lambda Handler function signature

func Wrap added in v1.21.0

func Wrap(host string, handler http.Handler) Handler

Wrap takes a HTTP handler and transforms it into an API Gateway Lambda Handler

type ResponseWriter

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

ResponseWriter implements the http.ResponseWriter interface in order to support the API Gateway Lambda HTTP "protocol".

func NewResponse

func NewResponse() *ResponseWriter

NewResponse returns a new response writer to capture http output.

func (*ResponseWriter) CloseNotify

func (w *ResponseWriter) CloseNotify() <-chan bool

CloseNotify notify when the response is closed

func (*ResponseWriter) End

End the request.

func (*ResponseWriter) Header

func (w *ResponseWriter) Header() http.Header

Header implementation.

func (*ResponseWriter) Write

func (w *ResponseWriter) Write(b []byte) (int, error)

Write implementation.

func (*ResponseWriter) WriteHeader

func (w *ResponseWriter) WriteHeader(status int)

WriteHeader implementation.

Jump to

Keyboard shortcuts

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