Skip to content

Instantly share code, notes, and snippets.

@antklim
Created November 19, 2020 03:38
Show Gist options
  • Select an option

  • Save antklim/2d9a6e90348a2f3c9a7b283e6f4b5c2e to your computer and use it in GitHub Desktop.

Select an option

Save antklim/2d9a6e90348a2f3c9a7b283e6f4b5c2e to your computer and use it in GitHub Desktop.
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
writeRawBody(w, r, notFoundResponse, http.StatusNotFound)
}
func doHandler() http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var req Request
if err := unmarshalBody(r, &req); err != nil {
log.Println(err)
response := NewErrorResponse(err)
writeBody(w, r, response, http.StatusBadRequest)
return
}
if err := req.Validate(); err != nil {
log.Println(err)
response := NewErrorResponse(err)
writeBody(w, r, response, http.StatusUnprocessableEntity)
return
}
operation := Operations[req.Operation]
result := Do(operation.lambda, req.Arguments)
response := Response{
Operation: req.Operation,
Arguments: req.Arguments,
Result: result,
}
writeBody(w, r, response, http.StatusOK)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment