Skip to content

Instantly share code, notes, and snippets.

@hugows
Last active July 17, 2017 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hugows/f794774dd787ff9cc8e63c1e8083f001 to your computer and use it in GitHub Desktop.
Save hugows/f794774dd787ff9cc8e63c1e8083f001 to your computer and use it in GitHub Desktop.
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