No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

render.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package phi
  2. import (
  3. "encoding/json"
  4. "encoding/xml"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. )
  10. // Content render errors.
  11. var (
  12. ErrMultipart = errors.New(`phi: received multiple parts`)
  13. ErrUnsupportedType = `phi: unsupported text type %T`
  14. )
  15. // Error returns a HTTP error.
  16. func Error(ctx Context, v interface{}) {
  17. var (
  18. code = http.StatusInternalServerError
  19. text = http.StatusText(code)
  20. )
  21. switch t := v.(type) {
  22. case error:
  23. text = t.Error()
  24. case int:
  25. code = t
  26. if code == http.StatusNoContent {
  27. text = ""
  28. } else {
  29. text = http.StatusText(t)
  30. }
  31. case []byte:
  32. text = string(t)
  33. case string:
  34. text = t
  35. }
  36. if len(text) > 0 {
  37. ctx.Header().Set("Content-Type", "text/plain; charset=utf-8")
  38. }
  39. ctx.WriteHeader(code)
  40. ctx.Write([]byte(text))
  41. }
  42. // Data returns binary content.
  43. func Data(ctx Context, v interface{}) error {
  44. ctx.Header().Set("Content-Type", "application/octet-stream")
  45. var data []byte
  46. switch t := v.(type) {
  47. case nil:
  48. Error(ctx, http.StatusNoContent)
  49. case []byte:
  50. data = t
  51. case string:
  52. data = []byte(t)
  53. case io.Reader:
  54. _, err := io.Copy(ctx, t)
  55. return err
  56. default:
  57. return fmt.Errorf(ErrUnsupportedType, t)
  58. }
  59. _, err := ctx.Write(data)
  60. return err
  61. }
  62. // Text returns plain text content.
  63. func Text(ctx Context, v interface{}) error {
  64. ctx.Header().Set("Content-Type", "text/plain; charset=utf-8")
  65. var text string
  66. switch t := v.(type) {
  67. case nil:
  68. Error(ctx, http.StatusNoContent)
  69. case []byte:
  70. text = string(t)
  71. case string:
  72. text = t
  73. case io.Reader:
  74. _, err := io.Copy(ctx, t)
  75. return err
  76. default:
  77. return fmt.Errorf(ErrUnsupportedType, t)
  78. }
  79. _, err := ctx.Write([]byte(text))
  80. return err
  81. }
  82. // HTML returns HTML content.
  83. func HTML(ctx Context, v interface{}) error {
  84. ctx.Header().Set("Content-Type", "text/html; charset=utf-8")
  85. var html string
  86. switch t := v.(type) {
  87. case nil:
  88. Error(ctx, http.StatusNoContent)
  89. case []byte:
  90. html = string(t)
  91. case string:
  92. html = t
  93. case io.Reader:
  94. _, err := io.Copy(ctx, t)
  95. return err
  96. default:
  97. return fmt.Errorf(ErrUnsupportedType, t)
  98. }
  99. _, err := ctx.Write([]byte(html))
  100. return err
  101. }
  102. // JSON returns JSON content.
  103. func JSON(ctx Context, v interface{}) error {
  104. ctx.Header().Set("Content-Type", "application/json; charset=utf-8")
  105. enc := json.NewEncoder(ctx)
  106. return enc.Encode(v)
  107. }
  108. // XML returns XML content.
  109. func XML(ctx Context, v interface{}) error {
  110. ctx.Header().Set("Content-Type", "application/xml; charset=utf-8")
  111. ctx.Write([]byte(xml.Header))
  112. enc := xml.NewEncoder(ctx)
  113. enc.Indent("", " ")
  114. return enc.Encode(v)
  115. }
  116. // Negotiated inspects the HTTP Accept header sent by the client and renders a response for the negotiated Content-Type if available.
  117. func Negotiated(ctx Context, v interface{}, defaultContentType string) error {
  118. driversMutex.RLock()
  119. defer driversMutex.RUnlock()
  120. var offers []string
  121. for contentType := range drivers {
  122. offers = append(offers, contentType)
  123. }
  124. negotiated := NegotiateContentType(ctx, offers, defaultContentType)
  125. if renderer, ok := drivers[negotiated]; ok {
  126. return renderer.Render(ctx, v)
  127. }
  128. return fmt.Errorf(`phi: no driver for content type %s`, defaultContentType)
  129. }
  130. func init() {
  131. Register(RenderFunc(Data), "application/octet-stream")
  132. Register(RenderFunc(JSON), "application/json")
  133. Register(RenderFunc(Text), "text/plain")
  134. Register(RenderFunc(XML), "application/xml", "text/xml")
  135. }