respond.go 680 B

12345678910111213141516171819202122232425262728
  1. package web
  2. import (
  3. "log"
  4. "github.com/gin-gonic/gin"
  5. r "github.com/gin-gonic/gin/render"
  6. )
  7. // render is a wrapper function that decides how to render a response, renders
  8. // the response, and can be extended to include additional tasks when rendering
  9. // for debug purposes, such as logging failures and contextually including extra
  10. // information.
  11. func render(c *gin.Context, status int, data interface{}) {
  12. var renderer r.Render
  13. if gin.IsDebugging() {
  14. renderer = r.IndentedJSON{Data: data}
  15. } else {
  16. renderer = r.JSON{Data: data}
  17. }
  18. if status > 400 {
  19. log.Printf("Failed to process request\tstatus: %d\tdata: %+v", status, data)
  20. }
  21. c.Render(status, renderer)
  22. }