Last active
July 17, 2017 13:38
-
-
Save hugows/f794774dd787ff9cc8e63c1e8083f001 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
type AppResponse struct { | |
Error error `json:"-"` | |
Code int `json:"code,omitempty"` | |
Message string `json:"message,omitempty"` | |
Data interface{} `json:"data,omitempty"` | |
} | |
type apiHandler func(w http.ResponseWriter, r *http.Request) AppResponse | |
func (handler apiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
result := handler(w, r) | |
if result.Error != nil { | |
result.Message = result.Error.Error() | |
render.JSON(w, http.StatusInternalServerError, result) | |
} else { | |
render.JSON(w, http.StatusOK, result) | |
} | |
} | |
func someHandler(w http.ResponseWriter, r *http.Request) AppResponse { | |
// Then we can simply return anywhere... | |
// This would be for a CREATE that needs no data in the response. | |
return AppResponse{ | |
Message: "ok", | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment