mirror of https://github.com/gin-gonic/gin.git
Performance improvement in renders
This commit is contained in:
parent
d6771dc4a5
commit
a9dad532ae
|
@ -21,14 +21,14 @@ type (
|
|||
)
|
||||
|
||||
func (html HTMLRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
|
||||
WriteHeader(w, code, "text/html")
|
||||
writeHeader(w, code, "text/html; charset=utf-8")
|
||||
file := data[0].(string)
|
||||
args := data[1]
|
||||
return html.Template.ExecuteTemplate(w, file, args)
|
||||
}
|
||||
|
||||
func (r *HTMLDebugRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
|
||||
WriteHeader(w, code, "text/html")
|
||||
writeHeader(w, code, "text/html; charset=utf-8")
|
||||
file := data[0].(string)
|
||||
obj := data[1]
|
||||
|
||||
|
@ -57,7 +57,7 @@ func (_ htmlPlainRender) Render(w http.ResponseWriter, code int, data ...interfa
|
|||
}
|
||||
|
||||
func WriteHTMLString(w http.ResponseWriter, code int, format string, values []interface{}) {
|
||||
WriteHeader(w, code, "text/html")
|
||||
writeHeader(w, code, "text/html; charset=utf-8")
|
||||
if len(values) > 0 {
|
||||
fmt.Fprintf(w, format, values...)
|
||||
} else {
|
||||
|
|
|
@ -16,16 +16,20 @@ func (_ jsonRender) Render(w http.ResponseWriter, code int, data ...interface{})
|
|||
}
|
||||
|
||||
func (_ indentedJSON) Render(w http.ResponseWriter, code int, data ...interface{}) error {
|
||||
WriteHeader(w, code, "application/json")
|
||||
jsonData, err := json.MarshalIndent(data[0], "", " ")
|
||||
return WriteIndentedJSON(w, code, data[0])
|
||||
}
|
||||
|
||||
func WriteJSON(w http.ResponseWriter, code int, data interface{}) error {
|
||||
writeHeader(w, code, "application/json; charset=utf-8")
|
||||
return json.NewEncoder(w).Encode(data)
|
||||
}
|
||||
|
||||
func WriteIndentedJSON(w http.ResponseWriter, code int, data interface{}) error {
|
||||
writeHeader(w, code, "application/json; charset=utf-8")
|
||||
jsonData, err := json.MarshalIndent(data, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = w.Write(jsonData)
|
||||
return err
|
||||
}
|
||||
|
||||
func WriteJSON(w http.ResponseWriter, code int, data interface{}) error {
|
||||
WriteHeader(w, code, "application/json")
|
||||
return json.NewEncoder(w).Encode(data)
|
||||
}
|
||||
|
|
|
@ -22,28 +22,7 @@ var (
|
|||
_ Render = &HTMLDebugRender{}
|
||||
)
|
||||
|
||||
func WriteHeader(w http.ResponseWriter, code int, contentType string) {
|
||||
contentType = joinStrings(contentType, "; charset=utf-8")
|
||||
func writeHeader(w http.ResponseWriter, code int, contentType string) {
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
w.WriteHeader(code)
|
||||
}
|
||||
|
||||
func joinStrings(a ...string) string {
|
||||
if len(a) == 0 {
|
||||
return ""
|
||||
}
|
||||
if len(a) == 1 {
|
||||
return a[0]
|
||||
}
|
||||
n := 0
|
||||
for i := 0; i < len(a); i++ {
|
||||
n += len(a[i])
|
||||
}
|
||||
|
||||
b := make([]byte, n)
|
||||
n = 0
|
||||
for _, s := range a {
|
||||
n += copy(b[n:], s)
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
|
|
@ -145,9 +145,3 @@ func TestRenderHTMLTemplate(t *testing.T) {
|
|||
assert.Equal(t, w.Body.String(), "Hello alexandernyquist")
|
||||
assert.Equal(t, w.Header().Get("Content-Type"), "text/html; charset=utf-8")
|
||||
}
|
||||
|
||||
func TestRenderJoinStrings(t *testing.T) {
|
||||
assert.Equal(t, joinStrings("a", "BB", "c"), "aBBc")
|
||||
assert.Equal(t, joinStrings("a", "", "c"), "ac")
|
||||
assert.Equal(t, joinStrings("text/html", "; charset=utf-8"), "text/html; charset=utf-8")
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ func (_ plainTextRender) Render(w http.ResponseWriter, code int, data ...interfa
|
|||
}
|
||||
|
||||
func WritePlainText(w http.ResponseWriter, code int, format string, values []interface{}) {
|
||||
WriteHeader(w, code, "text/plain")
|
||||
writeHeader(w, code, "text/plain; charset=utf-8")
|
||||
// we assume w.Write can not fail, is that right?
|
||||
if len(values) > 0 {
|
||||
fmt.Fprintf(w, format, values...)
|
||||
|
|
|
@ -12,6 +12,6 @@ func (_ xmlRender) Render(w http.ResponseWriter, code int, data ...interface{})
|
|||
}
|
||||
|
||||
func WriteXML(w http.ResponseWriter, code int, data interface{}) error {
|
||||
WriteHeader(w, code, "application/xml")
|
||||
writeHeader(w, code, "application/xml; charset=utf-8")
|
||||
return xml.NewEncoder(w).Encode(data)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue