Skip to content

Instantly share code, notes, and snippets.

@mweagle mweagle/serveHTTP.go
Created Apr 25, 2016

Embed
What would you like to do?
func (handler *LambdaHTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// Remove the leading slash and dispatch it to the golang handler
lambdaFunc := strings.TrimLeft(req.URL.Path, "/")
decoder := json.NewDecoder(req.Body)
var request lambdaRequest
defer func() {
if r := recover(); r != nil {
err, ok := r.(error)
if !ok {
err = fmt.Errorf("%v", r)
}
errorString := fmt.Sprintf("Lambda handler panic: %#v", err)
http.Error(w, errorString, http.StatusBadRequest)
}
}()
err := decoder.Decode(&request)
if nil != err {
errorString := fmt.Sprintf("Failed to decode proxy request: %s", err.Error())
http.Error(w, errorString, http.StatusBadRequest)
return
}
handler.logger.WithFields(logrus.Fields{
"Request": request,
"LookupName": lambdaFunc,
}).Debug("Dispatching")
lambdaAWSInfo := handler.lambdaDispatchMap[lambdaFunc]
var lambdaFn LambdaFunction
if nil != lambdaAWSInfo {
lambdaFn = lambdaAWSInfo.lambdaFn
} else if strings.Contains(lambdaFunc, "::") {
// Not the most exhaustive guard, but the CloudFormation custom resources
// all have "::" delimiters in their type field. Even if there is a false
// positive, the customResourceForwarder will simply error out.
lambdaFn = customResourceForwarder
}
if nil == lambdaFn {
http.Error(w, "Unsupported path: "+lambdaFunc, http.StatusBadRequest)
return
}
lambdaFn(&request.Event, &request.Context, w, handler.logger)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.