Created
November 19, 2020 03:38
-
-
Save antklim/2d9a6e90348a2f3c9a7b283e6f4b5c2e 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 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