2014-08-29 21:49:50 +04: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 19:41:56 +04:00
|
|
|
package render
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Render interface {
|
|
|
|
Render(http.ResponseWriter, int, ...interface{}) error
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonRender struct{}
|
|
|
|
|
2015-03-31 19:02:38 +03:00
|
|
|
indentedJSON struct{}
|
|
|
|
|
2014-07-15 19:41:56 +04:00
|
|
|
xmlRender struct{}
|
|
|
|
|
2015-03-23 06:42:05 +03:00
|
|
|
plainTextRender struct{}
|
2014-07-15 19:41:56 +04:00
|
|
|
|
2015-03-08 19:50:58 +03:00
|
|
|
htmlPlainRender struct{}
|
|
|
|
|
2014-07-29 02:48:02 +04:00
|
|
|
redirectRender struct{}
|
|
|
|
|
2014-07-15 19:41:56 +04:00
|
|
|
HTMLRender struct {
|
|
|
|
Template *template.Template
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-03-31 19:02:38 +03:00
|
|
|
JSON = jsonRender{}
|
|
|
|
IndentedJSON = indentedJSON{}
|
|
|
|
XML = xmlRender{}
|
|
|
|
HTMLPlain = htmlPlainRender{}
|
|
|
|
Plain = plainTextRender{}
|
|
|
|
Redirect = redirectRender{}
|
2014-07-15 19:41:56 +04:00
|
|
|
)
|
|
|
|
|
2014-07-29 02:48:02 +04:00
|
|
|
func (_ redirectRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
|
2015-04-07 13:27:02 +03:00
|
|
|
req := data[0].(*http.Request)
|
|
|
|
location := data[1].(string)
|
|
|
|
http.Redirect(w, req, location, code)
|
2014-07-29 02:48:02 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-31 18:36:13 +03:00
|
|
|
func (_ jsonRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
|
|
|
|
WriteHeader(w, code, "application/json")
|
|
|
|
return json.NewEncoder(w).Encode(data[0])
|
|
|
|
}
|
|
|
|
|
2015-03-31 19:02:38 +03:00
|
|
|
func (_ indentedJSON) Render(w http.ResponseWriter, code int, data ...interface{}) error {
|
|
|
|
WriteHeader(w, code, "application/json")
|
2015-03-31 19:04:52 +03:00
|
|
|
jsonData, err := json.MarshalIndent(data[0], "", " ")
|
2015-03-31 19:02:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = w.Write(jsonData)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-07-15 19:41:56 +04:00
|
|
|
func (_ xmlRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
|
2015-03-31 18:36:13 +03:00
|
|
|
WriteHeader(w, code, "application/xml")
|
|
|
|
return xml.NewEncoder(w).Encode(data[0])
|
2014-07-15 19:41:56 +04:00
|
|
|
}
|
|
|
|
|
2015-03-23 06:42:05 +03:00
|
|
|
func (_ plainTextRender) Render(w http.ResponseWriter, code int, data ...interface{}) (err error) {
|
2015-03-31 18:36:13 +03:00
|
|
|
WriteHeader(w, code, "text/plain")
|
2014-07-15 19:41:56 +04:00
|
|
|
format := data[0].(string)
|
|
|
|
args := data[1].([]interface{})
|
2015-03-08 19:50:58 +03:00
|
|
|
if len(args) > 0 {
|
|
|
|
_, err = w.Write([]byte(fmt.Sprintf(format, args...)))
|
|
|
|
} else {
|
|
|
|
_, err = w.Write([]byte(format))
|
|
|
|
}
|
2015-03-23 06:42:05 +03:00
|
|
|
return
|
2015-03-08 19:50:58 +03:00
|
|
|
}
|
|
|
|
|
2015-03-31 18:36:13 +03:00
|
|
|
func (_ htmlPlainRender) Render(w http.ResponseWriter, code int, data ...interface{}) (err error) {
|
|
|
|
WriteHeader(w, code, "text/html")
|
2015-03-08 19:50:58 +03:00
|
|
|
format := data[0].(string)
|
|
|
|
args := data[1].([]interface{})
|
2014-07-18 03:56:59 +04:00
|
|
|
if len(args) > 0 {
|
|
|
|
_, err = w.Write([]byte(fmt.Sprintf(format, args...)))
|
|
|
|
} else {
|
|
|
|
_, err = w.Write([]byte(format))
|
|
|
|
}
|
2015-03-31 18:36:13 +03:00
|
|
|
return
|
2014-07-15 19:41:56 +04:00
|
|
|
}
|
2014-08-20 20:14:10 +04:00
|
|
|
|
2015-03-31 18:36:13 +03:00
|
|
|
func (html HTMLRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
|
|
|
|
WriteHeader(w, code, "text/html")
|
|
|
|
file := data[0].(string)
|
|
|
|
args := data[1]
|
|
|
|
return html.Template.ExecuteTemplate(w, file, args)
|
2014-09-17 07:33:13 +04:00
|
|
|
}
|
|
|
|
|
2015-03-31 18:36:13 +03: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 07:33:13 +04:00
|
|
|
}
|
|
|
|
|
2015-03-31 18:36:13 +03:00
|
|
|
func joinStrings(a ...string) string {
|
|
|
|
if len(a) == 0 {
|
|
|
|
return ""
|
2014-08-21 03:04:35 +04:00
|
|
|
}
|
2015-03-31 18:36:13 +03:00
|
|
|
if len(a) == 1 {
|
|
|
|
return a[0]
|
|
|
|
}
|
|
|
|
n := 0
|
|
|
|
for i := 0; i < len(a); i++ {
|
|
|
|
n += len(a[i])
|
2014-09-17 07:33:13 +04:00
|
|
|
}
|
|
|
|
|
2015-03-31 18:36:13 +03:00
|
|
|
b := make([]byte, n)
|
|
|
|
n = 0
|
|
|
|
for _, s := range a {
|
|
|
|
n += copy(b[n:], s)
|
|
|
|
}
|
|
|
|
return string(b)
|
2014-08-20 20:14:10 +04:00
|
|
|
}
|