Skip to content

Instantly share code, notes, and snippets.

@bradrydzewski
Created April 13, 2018 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradrydzewski/949b998c5f2b77acb5460e80c2e5f44b to your computer and use it in GitHub Desktop.
Save bradrydzewski/949b998c5f2b77acb5460e80c2e5f44b to your computer and use it in GitHub Desktop.
AWS Lambda Shim
package main
import (
"errors"
"fmt"
"log"
"net/http"
"net/url"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
func Handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
r := ToRequest(request)
w := &ResponseWriter{}
http.DefaultServeMux.ServeHTTP(w, r)
return w.ToResponse()
}
func ToRequest(r events.APIGatewayProxyRequest) *http.Request {
return &http.Request{
Method: "POST",
URL: url.URL{Path: r.Path},
Proto: "HTTP/1.0",
ProtoMajor: 1,
ProtoMinor: 0,
Header: r.Header,
Body: strings.NewReader(r.Body),
ContentLength: len(r.Body),
}
}
type ResponseWriter struct {
header http.Header
status int
body bytes.Buffer
}
func (w *ResponseWriter) Header() http.Header {
return w.header
}
func (w *ResponseWriter) Write(b []byte) (int, error) {
return w.body.Write(b)
}
func (w *ResponseWriter) WriteHeader(statusCode int) {
w.status = statusCode
}
func (w *ResponseWriter) ToResponse() events.APIGatewayProxyResponse {
return &events.APIGatewayProxyResponse{
StatusCode: w.status,
Headers: w.header,
Body: w.body.String(),
IsBase64Encoded: false,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment