gin/render/render.go

50 lines
1.1 KiB
Go
Raw Normal View History

2014-08-29 17:49:50 +00:00
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
2014-07-15 15:41:56 +00:00
package render
import "net/http"
type Render interface {
Render(http.ResponseWriter, int, ...interface{}) error
}
2014-07-15 15:41:56 +00:00
var (
JSON Render = jsonRender{}
IndentedJSON Render = indentedJSON{}
XML Render = xmlRender{}
HTMLPlain Render = htmlPlainRender{}
Plain Render = plainTextRender{}
Redirect Render = redirectRender{}
Data Render = dataRender{}
_ Render = HTMLRender{}
_ Render = &HTMLDebugRender{}
2014-07-15 15:41:56 +00:00
)
func WriteHeader(w http.ResponseWriter, code int, contentType string) {
contentType = joinStrings(contentType, "; charset=utf-8")
w.Header().Set("Content-Type", contentType)
w.WriteHeader(code)
}
func joinStrings(a ...string) string {
if len(a) == 0 {
return ""
2014-08-20 23:04:35 +00:00
}
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)
}