Created
April 25, 2016 20:30
-
-
Save mweagle/c52707a84488eba7106da14aa8e2a15d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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