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 (
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Render interface {
|
|
|
|
Render(http.ResponseWriter, int, ...interface{}) error
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonRender struct{}
|
|
|
|
|
|
|
|
xmlRender struct{}
|
|
|
|
|
2015-03-23 03:42:05 +00:00
|
|
|
plainTextRender struct{}
|
2014-07-15 15:41:56 +00:00
|
|
|
|
2015-03-08 16:50:58 +00:00
|
|
|
htmlPlainRender struct{}
|
|
|
|
|
2014-07-28 22:48:02 +00:00
|
|
|
redirectRender struct{}
|
|
|
|
|
2014-09-17 03:33:13 +00:00
|
|
|
htmlDebugRender struct {
|
|
|
|
files []string
|
|
|
|
globs []string
|
|
|
|
}
|
2014-08-20 16:14:10 +00:00
|
|
|
|
2014-07-15 15:41:56 +00:00
|
|
|
HTMLRender struct {
|
|
|
|
Template *template.Template
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-08-20 16:14:10 +00:00
|
|
|
JSON = jsonRender{}
|
|
|
|
XML = xmlRender{}
|
2015-03-08 16:50:58 +00:00
|
|
|
HTMLPlain = htmlPlainRender{}
|
2015-03-23 03:42:05 +00:00
|
|
|
Plain = plainTextRender{}
|
2014-08-20 16:14:10 +00:00
|
|
|
Redirect = redirectRender{}
|
2014-09-17 03:33:13 +00:00
|
|
|
HTMLDebug = &htmlDebugRender{}
|
2014-07-15 15:41:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func writeHeader(w http.ResponseWriter, code int, contentType string) {
|
2015-01-30 02:14:09 +00:00
|
|
|
w.Header().Set("Content-Type", contentType+"; charset=utf-8")
|
2014-08-18 03:24:48 +00:00
|
|
|
w.WriteHeader(code)
|
2014-07-15 15:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (_ jsonRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
|
2014-07-16 00:49:22 +00:00
|
|
|
writeHeader(w, code, "application/json")
|
2014-07-15 15:41:56 +00:00
|
|
|
encoder := json.NewEncoder(w)
|
|
|
|
return encoder.Encode(data[0])
|
|
|
|
}
|
|
|
|
|
2014-07-28 22:48:02 +00:00
|
|
|
func (_ redirectRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
|
|
|
|
w.Header().Set("Location", data[0].(string))
|
2014-07-28 22:53:56 +00:00
|
|
|
w.WriteHeader(code)
|
2014-07-28 22:48:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-15 15:41:56 +00:00
|
|
|
func (_ xmlRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
|
|
|
|
writeHeader(w, code, "application/xml")
|
|
|
|
encoder := xml.NewEncoder(w)
|
|
|
|
return encoder.Encode(data[0])
|
|
|
|
}
|
|
|
|
|
2015-03-23 03:42:05 +00:00
|
|
|
func (_ plainTextRender) Render(w http.ResponseWriter, code int, data ...interface{}) (err error) {
|
2014-07-15 15:41:56 +00:00
|
|
|
writeHeader(w, code, "text/plain")
|
|
|
|
format := data[0].(string)
|
|
|
|
args := data[1].([]interface{})
|
2015-03-08 16:50:58 +00:00
|
|
|
if len(args) > 0 {
|
|
|
|
_, err = w.Write([]byte(fmt.Sprintf(format, args...)))
|
|
|
|
} else {
|
|
|
|
_, err = w.Write([]byte(format))
|
|
|
|
}
|
2015-03-23 03:42:05 +00:00
|
|
|
return
|
2015-03-08 16:50:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (_ htmlPlainRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
|
|
|
|
writeHeader(w, code, "text/html")
|
|
|
|
format := data[0].(string)
|
|
|
|
args := data[1].([]interface{})
|
2014-07-17 23:56:59 +00:00
|
|
|
var err error
|
|
|
|
if len(args) > 0 {
|
|
|
|
_, err = w.Write([]byte(fmt.Sprintf(format, args...)))
|
|
|
|
} else {
|
|
|
|
_, err = w.Write([]byte(format))
|
|
|
|
}
|
2014-07-15 15:41:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-08-20 16:14:10 +00:00
|
|
|
|
2014-09-17 03:33:13 +00:00
|
|
|
func (r *htmlDebugRender) AddGlob(pattern string) {
|
|
|
|
r.globs = append(r.globs, pattern)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *htmlDebugRender) AddFiles(files ...string) {
|
|
|
|
r.files = append(r.files, files...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *htmlDebugRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
|
2014-08-20 16:14:10 +00:00
|
|
|
writeHeader(w, code, "text/html")
|
|
|
|
file := data[0].(string)
|
|
|
|
obj := data[1]
|
2014-09-17 03:33:13 +00:00
|
|
|
|
|
|
|
t := template.New("")
|
|
|
|
|
|
|
|
if len(r.files) > 0 {
|
|
|
|
if _, err := t.ParseFiles(r.files...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-08-20 23:04:35 +00:00
|
|
|
}
|
2014-09-17 03:33:13 +00:00
|
|
|
|
|
|
|
for _, glob := range r.globs {
|
2014-09-17 03:44:42 +00:00
|
|
|
if _, err := t.ParseGlob(glob); err != nil {
|
2014-09-17 03:33:13 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-20 23:04:35 +00:00
|
|
|
return t.ExecuteTemplate(w, file, obj)
|
2014-08-20 16:14:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (html HTMLRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
|
|
|
|
writeHeader(w, code, "text/html")
|
|
|
|
file := data[0].(string)
|
|
|
|
obj := data[1]
|
|
|
|
return html.Template.ExecuteTemplate(w, file, obj)
|
|
|
|
}
|