json.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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
  5. import (
  6. "encoding/json"
  7. "errors"
  8. "net/http"
  9. "github.com/miniflux/miniflux/logger"
  10. )
  11. // OK sends a JSON response with the status code 200.
  12. func OK(w http.ResponseWriter, v interface{}) {
  13. commonHeaders(w)
  14. w.WriteHeader(http.StatusOK)
  15. w.Write(toJSON(v))
  16. }
  17. // Created sends a JSON response with the status code 201.
  18. func Created(w http.ResponseWriter, v interface{}) {
  19. commonHeaders(w)
  20. w.WriteHeader(http.StatusCreated)
  21. w.Write(toJSON(v))
  22. }
  23. // NoContent sends a JSON response with the status code 204.
  24. func NoContent(w http.ResponseWriter) {
  25. commonHeaders(w)
  26. w.WriteHeader(http.StatusNoContent)
  27. }
  28. // NotFound sends a JSON response with the status code 404.
  29. func NotFound(w http.ResponseWriter, err error) {
  30. logger.Error("[Not Found] %v", err)
  31. commonHeaders(w)
  32. w.WriteHeader(http.StatusNotFound)
  33. w.Write(encodeError(err))
  34. }
  35. // ServerError sends a JSON response with the status code 500.
  36. func ServerError(w http.ResponseWriter, err error) {
  37. logger.Error("[Internal Server Error] %v", err)
  38. commonHeaders(w)
  39. w.WriteHeader(http.StatusInternalServerError)
  40. if err != nil {
  41. w.Write(encodeError(err))
  42. }
  43. }
  44. // Forbidden sends a JSON response with the status code 403.
  45. func Forbidden(w http.ResponseWriter) {
  46. logger.Info("[Forbidden]")
  47. commonHeaders(w)
  48. w.WriteHeader(http.StatusForbidden)
  49. w.Write(encodeError(errors.New("Access Forbidden")))
  50. }
  51. // Unauthorized sends a JSON response with the status code 401.
  52. func Unauthorized(w http.ResponseWriter) {
  53. commonHeaders(w)
  54. w.WriteHeader(http.StatusUnauthorized)
  55. w.Write(encodeError(errors.New("Access Unauthorized")))
  56. }
  57. // BadRequest sends a JSON response with the status code 400.
  58. func BadRequest(w http.ResponseWriter, err error) {
  59. logger.Error("[Bad Request] %v", err)
  60. commonHeaders(w)
  61. w.WriteHeader(http.StatusBadRequest)
  62. if err != nil {
  63. w.Write(encodeError(err))
  64. }
  65. }
  66. func commonHeaders(w http.ResponseWriter) {
  67. w.Header().Set("Accept", "application/json")
  68. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  69. }
  70. func encodeError(err error) []byte {
  71. type errorMsg struct {
  72. ErrorMessage string `json:"error_message"`
  73. }
  74. tmp := errorMsg{ErrorMessage: err.Error()}
  75. data, err := json.Marshal(tmp)
  76. if err != nil {
  77. logger.Error("json encoding error: %v", err)
  78. }
  79. return data
  80. }
  81. func toJSON(v interface{}) []byte {
  82. b, err := json.Marshal(v)
  83. if err != nil {
  84. logger.Error("json encoding error: %v", err)
  85. return []byte("")
  86. }
  87. return b
  88. }