Categorygithub.com/Drafteame/gateway/v2
modulepackage
2.2.0
Repository: https://github.com/drafteame/gateway.git
Documentation: pkg.go.dev

# 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)
}

# Packages

No description provided by the author
No description provided by the author
nolint.
No description provided by the author

# 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.
WithDecorator adds a new decorator to the lambda handler configuration.

# Structs

Gateway wrap a http handler to enable use as a lambda.Handler.
Options represents all options that can be applied to the lambda handler.
ResponseWriter implements the http.ResponseWriter interface in order to support the API Gateway Lambda HTTP "protocol".

# Type aliases

Decorator is a wrapper function that adds functionality to the current lambda handler.
Option is a callback that configure som handler option.