Skip to content

earthboundkid/gateway

 
 

Repository files navigation

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

About

Drop-in replacement for Go net/http when running in AWS Lambda & API Gateway

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%