Core functionality for new aiterp.net servers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

26 lines
560 B

7 years ago
  1. package response
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. )
  8. // JSON makes a JSON response
  9. func JSON(writer http.ResponseWriter, status int, data interface{}) {
  10. jsonData, err := json.Marshal(data)
  11. if err != nil {
  12. log.Println("JSON Marshal failed: ", err.Error())
  13. writer.Header().Set("Content-Type", "text/plain; charset=utf-8")
  14. writer.WriteHeader(503)
  15. fmt.Fprint(writer, "JSON marshalling failed:", err.Error())
  16. return
  17. }
  18. writer.Header().Set("Content-Type", "application/json")
  19. writer.WriteHeader(status)
  20. writer.Write(jsonData)
  21. }