web: response by content-type
continuous-integration/drone the build is pending Details

This commit is contained in:
Geno 2021-07-18 02:14:59 +02:00
parent 7923008c9f
commit ffd77c3aab
1 changed files with 42 additions and 0 deletions

42
web/response.go Normal file
View File

@ -0,0 +1,42 @@
package web
import (
"github.com/gin-gonic/gin"
)
const (
// ContentTypeJSON content type of json
ContentTypeJSON = "application/json"
// ContentTypeJS content type of javascript
ContentTypeJS = "application/javascript"
// ContentTypeXML content type of xml
ContentTypeXML = "text/xml"
// ContentTypeYAML content type of yaml
ContentTypeYAML = "text/yaml"
// ContentTypeHTML content type of html
ContentTypeHTML = "text/html"
)
// Response give
func Response(ctx *gin.Context, statusCode int, data interface{}, templateName string) {
switch ctx.ContentType() {
case ContentTypeJS:
ctx.JSONP(statusCode, data)
return
case ContentTypeJSON:
ctx.JSON(statusCode, data)
return
case ContentTypeYAML:
ctx.YAML(statusCode, data)
return
case ContentTypeXML:
ctx.XML(statusCode, data)
return
case ContentTypeHTML:
ctx.HTML(statusCode, templateName, data)
return
default:
ctx.JSON(statusCode, data)
return
}
}