summaryrefslogtreecommitdiff
path: root/server/view.go
blob: 63d5314c084c817e761185e406b39e56fab7f5eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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)
}