Nenhuma descrição

response.go 578B

12345678910111213141516171819202122232425262728293031323334
  1. package response
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. )
  6. var responseCode = 200
  7. var responseWriter *http.ResponseWriter
  8. //JSON :response json
  9. func JSON(data interface{}) {
  10. w := *responseWriter
  11. js, err := json.Marshal(data)
  12. if err != nil {
  13. http.Error(w, err.Error(), http.StatusInternalServerError)
  14. return
  15. }
  16. w.Header().Set("Content-Type", "application/json")
  17. w.WriteHeader(responseCode)
  18. w.Write(js)
  19. }
  20. //SetCode :informa o código de resposta
  21. func SetCode(code int) {
  22. responseCode = code
  23. }
  24. func SetResponseWriter(w http.ResponseWriter) {
  25. responseWriter = &w
  26. }