gateway

package module
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2022 License: MIT Imports: 12 Imported by: 0

README

Gateway

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.

There are two versions of this library, version 1.x supports AWS API Gateway 1.0 events used by the original REST APIs, and 2.x which supports 2.0 events used by the HTTP APIs. For more information on the options read Choosing between HTTP APIs and REST APIs on the AWS documentation website.

Installation

To install version 1.x for REST APIs.

go get github.com/Drafteame/gateway/v2

Example

package main

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

	"github.com/Drafteame/gateway/v2"
)

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

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListenAndServe

func ListenAndServe(h http.Handler, opts ...Option) error

ListenAndServe is a drop-in replacement for http.ListenAndServe for use within AWS Lambda.

ListenAndServe always returns a non-nil error.

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 Decorator

type Decorator func(handler interface{}) interface{}

Decorator is a wrapper function that adds functionality to the current lambda handler.

type Gateway

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

Gateway wrap a http handler to enable use as a lambda.Handler

func NewGateway

func NewGateway(h http.Handler) *Gateway

NewGateway creates a gateway using the provided http.Handler enabling use in existing aws-lambda-go projects

type Option

type Option func(*Options)

Option is a callback that configure som handler option.

func WithDecorator

func WithDecorator(d Decorator) Option

WithDecorator adds a new decorator to the lambda handler configuration.

type Options

type Options struct {
	Decorators []Decorator
}

Options represents all options that can be applied to the lambda handler.

func (*Options) Apply

func (o *Options) Apply(handler interface{}) interface{}

Apply executes options over the 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.

Directories

Path Synopsis
nolint
nolint
pkg

Jump to

Keyboard shortcuts

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