utils.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package api
  2. import (
  3. "encoding/json"
  4. "io"
  5. "log"
  6. "net/http"
  7. "github.com/smallstep/certificates/errs"
  8. "github.com/smallstep/certificates/logging"
  9. "google.golang.org/protobuf/encoding/protojson"
  10. "google.golang.org/protobuf/proto"
  11. )
  12. // EnableLogger is an interface that enables response logging for an object.
  13. type EnableLogger interface {
  14. ToLog() (interface{}, error)
  15. }
  16. // LogError adds to the response writer the given error if it implements
  17. // logging.ResponseLogger. If it does not implement it, then writes the error
  18. // using the log package.
  19. func LogError(rw http.ResponseWriter, err error) {
  20. if rl, ok := rw.(logging.ResponseLogger); ok {
  21. rl.WithFields(map[string]interface{}{
  22. "error": err,
  23. })
  24. } else {
  25. log.Println(err)
  26. }
  27. }
  28. // LogEnabledResponse log the response object if it implements the EnableLogger
  29. // interface.
  30. func LogEnabledResponse(rw http.ResponseWriter, v interface{}) {
  31. if el, ok := v.(EnableLogger); ok {
  32. out, err := el.ToLog()
  33. if err != nil {
  34. LogError(rw, err)
  35. return
  36. }
  37. if rl, ok := rw.(logging.ResponseLogger); ok {
  38. rl.WithFields(map[string]interface{}{
  39. "response": out,
  40. })
  41. } else {
  42. log.Println(out)
  43. }
  44. }
  45. }
  46. // JSON writes the passed value into the http.ResponseWriter.
  47. func JSON(w http.ResponseWriter, v interface{}) {
  48. JSONStatus(w, v, http.StatusOK)
  49. }
  50. // JSONStatus writes the given value into the http.ResponseWriter and the
  51. // given status is written as the status code of the response.
  52. func JSONStatus(w http.ResponseWriter, v interface{}, status int) {
  53. w.Header().Set("Content-Type", "application/json")
  54. w.WriteHeader(status)
  55. if err := json.NewEncoder(w).Encode(v); err != nil {
  56. LogError(w, err)
  57. return
  58. }
  59. LogEnabledResponse(w, v)
  60. }
  61. // JSONNotFound writes a HTTP Not Found response with empty body.
  62. func JSONNotFound(w http.ResponseWriter) {
  63. w.Header().Set("Content-Type", "application/json")
  64. w.WriteHeader(http.StatusNotFound)
  65. LogEnabledResponse(w, nil)
  66. }
  67. // ProtoJSON writes the passed value into the http.ResponseWriter.
  68. func ProtoJSON(w http.ResponseWriter, m proto.Message) {
  69. ProtoJSONStatus(w, m, http.StatusOK)
  70. }
  71. // ProtoJSONStatus writes the given value into the http.ResponseWriter and the
  72. // given status is written as the status code of the response.
  73. func ProtoJSONStatus(w http.ResponseWriter, m proto.Message, status int) {
  74. w.Header().Set("Content-Type", "application/json")
  75. w.WriteHeader(status)
  76. b, err := protojson.Marshal(m)
  77. if err != nil {
  78. LogError(w, err)
  79. return
  80. }
  81. if _, err := w.Write(b); err != nil {
  82. LogError(w, err)
  83. return
  84. }
  85. //LogEnabledResponse(w, v)
  86. }
  87. // ReadJSON reads JSON from the request body and stores it in the value
  88. // pointed by v.
  89. func ReadJSON(r io.Reader, v interface{}) error {
  90. if err := json.NewDecoder(r).Decode(v); err != nil {
  91. return errs.BadRequestErr(err, "error decoding json")
  92. }
  93. return nil
  94. }
  95. // ReadProtoJSON reads JSON from the request body and stores it in the value
  96. // pointed by v.
  97. func ReadProtoJSON(r io.Reader, m proto.Message) error {
  98. data, err := io.ReadAll(r)
  99. if err != nil {
  100. return errs.BadRequestErr(err, "error reading request body")
  101. }
  102. return protojson.Unmarshal(data, m)
  103. }