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
|
|
|
|
|
2015-05-07 10:44:52 +00:00
|
|
|
import "net/http"
|
2015-03-08 16:50:58 +00:00
|
|
|
|
2015-05-07 10:44:52 +00:00
|
|
|
type Render interface {
|
|
|
|
Render(http.ResponseWriter, int, ...interface{}) error
|
|
|
|
}
|
2014-07-15 15:41:56 +00:00
|
|
|
|
|
|
|
var (
|
2015-05-07 10:44:52 +00:00
|
|
|
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
|
|
|
)
|
|
|
|
|
2015-03-31 15:36:13 +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)
|
2014-09-17 03:33:13 +00:00
|
|
|
}
|
|
|
|
|
2015-03-31 15:36:13 +00:00
|
|
|
func joinStrings(a ...string) string {
|
|
|
|
if len(a) == 0 {
|
|
|
|
return ""
|
2014-08-20 23:04:35 +00:00
|
|
|
}
|
2015-03-31 15:36:13 +00:00
|
|
|
if len(a) == 1 {
|
|
|
|
return a[0]
|
|
|
|
}
|
|
|
|
n := 0
|
|
|
|
for i := 0; i < len(a); i++ {
|
|
|
|
n += len(a[i])
|
2014-09-17 03:33:13 +00:00
|
|
|
}
|
|
|
|
|
2015-03-31 15:36:13 +00:00
|
|
|
b := make([]byte, n)
|
|
|
|
n = 0
|
|
|
|
for _, s := range a {
|
|
|
|
n += copy(b[n:], s)
|
|
|
|
}
|
|
|
|
return string(b)
|
2014-08-20 16:14:10 +00:00
|
|
|
}
|