diff options
Diffstat (limited to 'server/view.go')
| -rw-r--r-- | server/view.go | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/server/view.go b/server/view.go new file mode 100644 index 0000000..63d5314 --- /dev/null +++ b/server/view.go @@ -0,0 +1,30 @@ +package server + +import ( + "encoding/json" + "net/http" +) + +func Response(w http.ResponseWriter, data map[string]interface{}, status ...int) { + var responseStatus int + if len(status) > 0 { + responseStatus = status[0] + } else { + responseStatus = http.StatusOK + } + + w.WriteHeader(responseStatus) + + resp, _ := json.Marshal( + map[string]interface{}{ + "success": responseStatus == http.StatusOK, + "status": responseStatus, + "data": data, + }) + + w.Write(resp) +} + +func ErrorResponse(w http.ResponseWriter, message string, status int) { + Response(w, map[string]interface{}{"error": message}, status) +} |
