43 lines
914 B
Go
43 lines
914 B
Go
package main
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type CSV interface {
|
|
CSV() []byte
|
|
}
|
|
|
|
func render(c *gin.Context, status int, response any, contentType *string) {
|
|
value := c.GetHeader("accept")
|
|
if contentType != nil {
|
|
switch *contentType {
|
|
case "xml":
|
|
value = "text/xml"
|
|
case "json":
|
|
value = "application/json"
|
|
case "csv":
|
|
value = "text/csv"
|
|
}
|
|
}
|
|
switch value {
|
|
case "text/csv":
|
|
if csvResponse, ok := response.(CSV); ok {
|
|
c.Data(200, value+"; charset=utf-8", csvResponse.CSV())
|
|
} else {
|
|
c.Header("content-type", "application/json; charset=utf-8")
|
|
c.JSON(status, response)
|
|
}
|
|
case "application/xml":
|
|
fallthrough
|
|
case "text/xml":
|
|
c.Header("content-type", value+"; charset=utf-8; header=present;")
|
|
c.XML(status, response)
|
|
case "application/json":
|
|
fallthrough
|
|
default:
|
|
c.Header("content-type", "application/json; charset=utf-8")
|
|
c.JSON(status, response)
|
|
}
|
|
}
|