Created
April 13, 2018 21:00
-
-
Save bradrydzewski/949b998c5f2b77acb5460e80c2e5f44b to your computer and use it in GitHub Desktop.
AWS Lambda Shim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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