-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrender_html.go
103 lines (87 loc) · 2.47 KB
/
render_html.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package gimlet
import (
"bytes"
"html/template"
"io"
"net/http"
"path/filepath"
"strings"
"sync"
"github.com/mongodb/grip"
"github.com/mongodb/grip/message"
)
type htmlRenderer struct {
cache map[string]*template.Template
mu sync.Mutex
opts RendererOptions
}
// NewHTMLRenderer returns a Renderer implementation that wraps
// html/template and provides caching and streaming to http responses.
func NewHTMLRenderer(opts RendererOptions) Renderer {
if opts.Encoding == "" {
opts.Encoding = "UTF-8"
}
return &htmlRenderer{
cache: map[string]*template.Template{},
opts: opts,
}
}
func (r *htmlRenderer) GetTemplate(filenames ...string) (RenderTemplate, error) {
var (
tmpl *template.Template
cacheKey string
err error
ok bool
)
if !r.opts.DisableCache {
// generate a cache key by joining filenames with null byte (can't appear in filenames)
cacheKey = strings.Join(filenames, "\x00")
if tmpl, ok = r.cache[cacheKey]; ok {
return tmpl.Clone()
}
}
// cache miss (or cache is turned off) - try to load the templates from the filesystem
r.mu.Lock()
defer r.mu.Unlock()
paths := make([]string, 0, len(filenames))
for _, v := range filenames {
paths = append(paths, filepath.Join(r.opts.Directory, v))
}
tmpl = template.New(cacheKey).Funcs(r.opts.Functions)
tmpl, err = tmpl.ParseFiles(paths...)
if err != nil {
return nil, err
}
if !r.opts.DisableCache {
r.cache[cacheKey] = tmpl
}
return tmpl.Clone()
}
func (r *htmlRenderer) Render(out io.Writer, data interface{}, entryPoint string, files ...string) error {
t, err := r.GetTemplate(files...)
if err != nil {
return err
}
return t.ExecuteTemplate(out, entryPoint, data)
}
func (r *htmlRenderer) WriteResponse(w http.ResponseWriter, status int, data interface{}, entryPoint string, files ...string) {
out := &bytes.Buffer{}
err := r.Render(out, data, entryPoint, files...)
if err != nil {
WriteTextInternalError(w, err)
return
}
w.Header().Set("Content-Type", "text/html; charset="+r.opts.Encoding)
w.WriteHeader(status)
_, _ = w.Write(out.Bytes())
}
func (r *htmlRenderer) Stream(w http.ResponseWriter, status int, data interface{}, entryPoint string, files ...string) {
w.Header().Set("Content-Type", "text/html; charset="+r.opts.Encoding)
w.WriteHeader(status)
grip.Error(message.WrapError(r.Render(w, data, entryPoint, files...), message.Fields{
"entry": entryPoint,
"files": files,
"operation": "stream rendering",
"mode": "text",
}))
}