gin/render/render.go

141 lines
3.1 KiB
Go
Raw Normal View History

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
}
// JSON binding
jsonRender struct{}
// XML binding
xmlRender struct{}
// Plain text
plainRender struct{}
// HTML Plain text
htmlPlainRender struct{}
2014-07-29 02:48:02 +04:00
// Redirects
redirectRender struct{}
// Redirects
htmlDebugRender struct {
files []string
globs []string
}
2014-07-15 19:41:56 +04:00
// form binding
HTMLRender struct {
Template *template.Template
}
)
var (
JSON = jsonRender{}
XML = xmlRender{}
Plain = plainRender{}
HTMLPlain = htmlPlainRender{}
Redirect = redirectRender{}
HTMLDebug = &htmlDebugRender{}
2014-07-15 19:41:56 +04:00
)
func writeHeader(w http.ResponseWriter, code int, contentType string) {
w.Header().Set("Content-Type", contentType+"; charset=utf-8")
2014-08-18 07:24:48 +04:00
w.WriteHeader(code)
2014-07-15 19:41:56 +04:00
}
func (_ jsonRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
writeHeader(w, code, "application/json")
2014-07-15 19:41:56 +04:00
encoder := json.NewEncoder(w)
return encoder.Encode(data[0])
}
2014-07-29 02:48:02 +04:00
func (_ redirectRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
w.Header().Set("Location", data[0].(string))
2014-07-29 02:53:56 +04:00
w.WriteHeader(code)
2014-07-29 02:48:02 +04:00
return nil
}
2014-07-15 19:41:56 +04: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])
}
func (_ plainRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
writeHeader(w, code, "text/plain")
format := data[0].(string)
args := data[1].([]interface{})
var err error
if len(args) > 0 {
_, err = w.Write([]byte(fmt.Sprintf(format, args...)))
} else {
_, err = w.Write([]byte(format))
}
return err
}
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-18 03:56:59 +04: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 19:41:56 +04:00
return err
}
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 {
writeHeader(w, code, "text/html")
file := data[0].(string)
obj := data[1]
t := template.New("")
if len(r.files) > 0 {
if _, err := t.ParseFiles(r.files...); err != nil {
return err
}
2014-08-21 03:04:35 +04:00
}
for _, glob := range r.globs {
2014-09-17 07:44:42 +04:00
if _, err := t.ParseGlob(glob); err != nil {
return err
}
}
2014-08-21 03:04:35 +04:00
return t.ExecuteTemplate(w, file, obj)
}
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)
}