json.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package json // import "miniflux.app/v2/internal/http/response/json"
  4. import (
  5. "encoding/json"
  6. "errors"
  7. "log/slog"
  8. "net/http"
  9. "miniflux.app/v2/internal/http/request"
  10. "miniflux.app/v2/internal/http/response"
  11. )
  12. const contentTypeHeader = `application/json`
  13. // OK creates a new JSON response with a 200 status code.
  14. func OK(w http.ResponseWriter, r *http.Request, body interface{}) {
  15. builder := response.New(w, r)
  16. builder.WithHeader("Content-Type", contentTypeHeader)
  17. builder.WithBody(toJSON(body))
  18. builder.Write()
  19. }
  20. // Created sends a created response to the client.
  21. func Created(w http.ResponseWriter, r *http.Request, body interface{}) {
  22. builder := response.New(w, r)
  23. builder.WithStatus(http.StatusCreated)
  24. builder.WithHeader("Content-Type", contentTypeHeader)
  25. builder.WithBody(toJSON(body))
  26. builder.Write()
  27. }
  28. // NoContent sends a no content response to the client.
  29. func NoContent(w http.ResponseWriter, r *http.Request) {
  30. builder := response.New(w, r)
  31. builder.WithStatus(http.StatusNoContent)
  32. builder.WithHeader("Content-Type", contentTypeHeader)
  33. builder.Write()
  34. }
  35. func Accepted(w http.ResponseWriter, r *http.Request) {
  36. builder := response.New(w, r)
  37. builder.WithStatus(http.StatusAccepted)
  38. builder.WithHeader("Content-Type", contentTypeHeader)
  39. builder.Write()
  40. }
  41. // ServerError sends an internal error to the client.
  42. func ServerError(w http.ResponseWriter, r *http.Request, err error) {
  43. slog.Error(http.StatusText(http.StatusInternalServerError),
  44. slog.Any("error", err),
  45. slog.String("client_ip", request.ClientIP(r)),
  46. slog.Group("request",
  47. slog.String("method", r.Method),
  48. slog.String("uri", r.RequestURI),
  49. slog.String("user_agent", r.UserAgent()),
  50. ),
  51. slog.Group("response",
  52. slog.Int("status_code", http.StatusInternalServerError),
  53. ),
  54. )
  55. builder := response.New(w, r)
  56. builder.WithStatus(http.StatusInternalServerError)
  57. builder.WithHeader("Content-Type", contentTypeHeader)
  58. builder.WithBody(toJSONError(err))
  59. builder.Write()
  60. }
  61. // BadRequest sends a bad request error to the client.
  62. func BadRequest(w http.ResponseWriter, r *http.Request, err error) {
  63. slog.Warn(http.StatusText(http.StatusBadRequest),
  64. slog.Any("error", err),
  65. slog.String("client_ip", request.ClientIP(r)),
  66. slog.Group("request",
  67. slog.String("method", r.Method),
  68. slog.String("uri", r.RequestURI),
  69. slog.String("user_agent", r.UserAgent()),
  70. ),
  71. slog.Group("response",
  72. slog.Int("status_code", http.StatusBadRequest),
  73. ),
  74. )
  75. builder := response.New(w, r)
  76. builder.WithStatus(http.StatusBadRequest)
  77. builder.WithHeader("Content-Type", contentTypeHeader)
  78. builder.WithBody(toJSONError(err))
  79. builder.Write()
  80. }
  81. // Unauthorized sends a not authorized error to the client.
  82. func Unauthorized(w http.ResponseWriter, r *http.Request) {
  83. slog.Warn(http.StatusText(http.StatusUnauthorized),
  84. slog.String("client_ip", request.ClientIP(r)),
  85. slog.Group("request",
  86. slog.String("method", r.Method),
  87. slog.String("uri", r.RequestURI),
  88. slog.String("user_agent", r.UserAgent()),
  89. ),
  90. slog.Group("response",
  91. slog.Int("status_code", http.StatusUnauthorized),
  92. ),
  93. )
  94. builder := response.New(w, r)
  95. builder.WithStatus(http.StatusUnauthorized)
  96. builder.WithHeader("Content-Type", contentTypeHeader)
  97. builder.WithBody(toJSONError(errors.New("access unauthorized")))
  98. builder.Write()
  99. }
  100. // Forbidden sends a forbidden error to the client.
  101. func Forbidden(w http.ResponseWriter, r *http.Request) {
  102. slog.Warn(http.StatusText(http.StatusForbidden),
  103. slog.String("client_ip", request.ClientIP(r)),
  104. slog.Group("request",
  105. slog.String("method", r.Method),
  106. slog.String("uri", r.RequestURI),
  107. slog.String("user_agent", r.UserAgent()),
  108. ),
  109. slog.Group("response",
  110. slog.Int("status_code", http.StatusForbidden),
  111. ),
  112. )
  113. builder := response.New(w, r)
  114. builder.WithStatus(http.StatusForbidden)
  115. builder.WithHeader("Content-Type", contentTypeHeader)
  116. builder.WithBody(toJSONError(errors.New("access forbidden")))
  117. builder.Write()
  118. }
  119. // NotFound sends a page not found error to the client.
  120. func NotFound(w http.ResponseWriter, r *http.Request) {
  121. slog.Warn(http.StatusText(http.StatusNotFound),
  122. slog.String("client_ip", request.ClientIP(r)),
  123. slog.Group("request",
  124. slog.String("method", r.Method),
  125. slog.String("uri", r.RequestURI),
  126. slog.String("user_agent", r.UserAgent()),
  127. ),
  128. slog.Group("response",
  129. slog.Int("status_code", http.StatusNotFound),
  130. ),
  131. )
  132. builder := response.New(w, r)
  133. builder.WithStatus(http.StatusNotFound)
  134. builder.WithHeader("Content-Type", contentTypeHeader)
  135. builder.WithBody(toJSONError(errors.New("resource not found")))
  136. builder.Write()
  137. }
  138. func toJSONError(err error) []byte {
  139. type errorMsg struct {
  140. ErrorMessage string `json:"error_message"`
  141. }
  142. return toJSON(errorMsg{ErrorMessage: err.Error()})
  143. }
  144. func toJSON(v interface{}) []byte {
  145. b, err := json.Marshal(v)
  146. if err != nil {
  147. slog.Error("Unable to marshal JSON response", slog.Any("error", err))
  148. return []byte("")
  149. }
  150. return b
  151. }