package response

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

var responseCode = 200

//JSON :response json
func JSON(w http.ResponseWriter, data interface{}) {
	js, err := json.Marshal(data)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(responseCode)
	w.Write(js)
}

//SetCode :informa o código de resposta
func SetCode(code int) {
	responseCode = code
}
