File: utils.go

package info (click to toggle)
garagemq 0.0~git20200204.15e6a9d%2Bds-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,836 kB
  • sloc: xml: 4,990; javascript: 989; makefile: 29
file content (20 lines) | stat: -rw-r--r-- 409 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package admin

import (
	"encoding/json"
	"net/http"
)

func JSONResponse(w http.ResponseWriter, data interface{}, code int) (int, error) {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	w.Header().Set("Access-Control-Allow-Origin", "*")
	w.WriteHeader(code)

	body, err := json.Marshal(data)
	if err != nil {
		body = []byte(
			`{"error": "Marshal error"}`)
	}

	return w.Write(body)
}