4
0

utils.go 2.9 KB

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