json.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package json // import "miniflux.app/http/response/json"
  5. import (
  6. "encoding/json"
  7. "errors"
  8. "net/http"
  9. "miniflux.app/http/response"
  10. "miniflux.app/logger"
  11. )
  12. // OK creates a new JSON response with a 200 status code.
  13. func OK(w http.ResponseWriter, r *http.Request, body interface{}) {
  14. builder := response.New(w, r)
  15. builder.WithHeader("Content-Type", "application/json; charset=utf-8")
  16. builder.WithBody(toJSON(body))
  17. builder.Write()
  18. }
  19. // Created sends a created response to the client.
  20. func Created(w http.ResponseWriter, r *http.Request, body interface{}) {
  21. builder := response.New(w, r)
  22. builder.WithStatus(http.StatusCreated)
  23. builder.WithHeader("Content-Type", "application/json; charset=utf-8")
  24. builder.WithBody(toJSON(body))
  25. builder.Write()
  26. }
  27. // NoContent sends a no content response to the client.
  28. func NoContent(w http.ResponseWriter, r *http.Request) {
  29. builder := response.New(w, r)
  30. builder.WithStatus(http.StatusNoContent)
  31. builder.WithHeader("Content-Type", "application/json; charset=utf-8")
  32. builder.Write()
  33. }
  34. // ServerError sends an internal error to the client.
  35. func ServerError(w http.ResponseWriter, r *http.Request, err error) {
  36. logger.Error("[HTTP:Internal Server Error] %s => %v", r.URL, err)
  37. builder := response.New(w, r)
  38. builder.WithStatus(http.StatusInternalServerError)
  39. builder.WithHeader("Content-Type", "application/json; charset=utf-8")
  40. builder.WithBody(toJSONError(err))
  41. builder.Write()
  42. }
  43. // BadRequest sends a bad request error to the client.
  44. func BadRequest(w http.ResponseWriter, r *http.Request, err error) {
  45. logger.Error("[HTTP:Bad Request] %s => %v", r.URL, err)
  46. builder := response.New(w, r)
  47. builder.WithStatus(http.StatusBadRequest)
  48. builder.WithHeader("Content-Type", "application/json; charset=utf-8")
  49. builder.WithBody(toJSONError(err))
  50. builder.Write()
  51. }
  52. // Unauthorized sends a not authorized error to the client.
  53. func Unauthorized(w http.ResponseWriter, r *http.Request) {
  54. logger.Error("[HTTP:Unauthorized] %s", r.URL)
  55. builder := response.New(w, r)
  56. builder.WithStatus(http.StatusUnauthorized)
  57. builder.WithHeader("Content-Type", "application/json; charset=utf-8")
  58. builder.WithBody(toJSONError(errors.New("Access Unauthorized")))
  59. builder.Write()
  60. }
  61. // Forbidden sends a forbidden error to the client.
  62. func Forbidden(w http.ResponseWriter, r *http.Request) {
  63. logger.Error("[HTTP:Forbidden] %s", r.URL)
  64. builder := response.New(w, r)
  65. builder.WithStatus(http.StatusForbidden)
  66. builder.WithHeader("Content-Type", "application/json; charset=utf-8")
  67. builder.WithBody(toJSONError(errors.New("Access Forbidden")))
  68. builder.Write()
  69. }
  70. // NotFound sends a page not found error to the client.
  71. func NotFound(w http.ResponseWriter, r *http.Request) {
  72. logger.Error("[HTTP:Not Found] %s", r.URL)
  73. builder := response.New(w, r)
  74. builder.WithStatus(http.StatusNotFound)
  75. builder.WithHeader("Content-Type", "application/json; charset=utf-8")
  76. builder.WithBody(toJSONError(errors.New("Resource Not Found")))
  77. builder.Write()
  78. }
  79. func toJSONError(err error) []byte {
  80. type errorMsg struct {
  81. ErrorMessage string `json:"error_message"`
  82. }
  83. return toJSON(errorMsg{ErrorMessage: err.Error()})
  84. }
  85. func toJSON(v interface{}) []byte {
  86. b, err := json.Marshal(v)
  87. if err != nil {
  88. logger.Error("[HTTP:JSON] %v", err)
  89. return []byte("")
  90. }
  91. return b
  92. }