Skip to content

Instantly share code, notes, and snippets.

@JaeTLDR
Last active July 15, 2020 10:49
Show Gist options
  • Save JaeTLDR/53833be5aaa5787a9df29a8380c8f1bf to your computer and use it in GitHub Desktop.
Save JaeTLDR/53833be5aaa5787a9df29a8380c8f1bf to your computer and use it in GitHub Desktop.
a very basic lambda proxy for local development, accepts -local flag to run the echo based proxy
package main
import (
"bytes"
"flag"
"fmt"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/labstack/echo"
)
var isLocal = flag.Bool("local", false, "is running the local runtime stack")
func HandleRequest(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
return events.APIGatewayProxyResponse{
StatusCode: http.StatusOk,
Body: req.body,
}
}
func lambdaProxy(c echo.Context) error {
buf := new(bytes.Buffer)
buf.ReadFrom(c.Request().Body)
bodyStr := buf.String()
var headerMap = map[string]string{}
header := c.Request().Header
for name, values := range header {
for _, value := range values {
headerMap[name] = value
}
}
var queryMap = map[string]string{}
query := c.Request().URL.Query()
for name, values := range query {
for _, value := range values {
queryMap[name] = value
}
}
req := events.APIGatewayProxyRequest{
Path: c.Request().URL.Path,
HTTPMethod: c.Request().Method,
Headers: headerMap,
QueryStringParameters: queryMap,
//PathParameters: c.Request().URL. NOT SUPPORTED
// StageVariables: NOT SUPPORTED
// RequestContext: NOT SUPPORTED
Body: bodyStr,
IsBase64Encoded: false,
}
resp, _ := HandleRequest(req)
return c.String(resp.StatusCode, resp.Body)
}
func main() {
flag.Parse()
if *isLocal {
fmt.Println("is local")
e := echo.New()
e.Any("/*", lambdaProxy)
e.Logger.Fatal(e.Start(":1323"))
return
}
lambda.Start(HandleRequest)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment