-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
118 lines (102 loc) · 2.84 KB
/
util.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package views
import (
"fmt"
"html/template"
"net/http"
"path"
"strings"
"github.com/gobuffalo/packd"
"go.uber.org/zap"
)
func (h *handlers) loadTemplates() error {
h.templates = make(map[string]*template.Template)
// load template files
layoutContent := ""
pageContents := map[string]string{}
err := h.opts.TemplatesBox.Walk(func(filepath string, file packd.File) error {
if strings.HasPrefix(path.Base(filepath), ".#") {
// ignore temporary files
return nil
}
switch path.Dir(filepath) {
case ".":
pageContents[filepath] = file.String()
case "layout":
layoutContent += file.String()
}
return nil
})
if err != nil {
return err
}
// generate optimized templates
h.funcmap = getFuncmap(h.opts)
mainTemplate := template.New("main").Funcs(h.funcmap.fm)
mainTemplate = template.Must(mainTemplate.Parse(`{{define "main"}}{{template "base" .}}{{end}}`))
mainTemplate = template.Must(mainTemplate.Parse(layoutContent))
for filepath, content := range pageContents {
h.templates[filepath] = template.Must(mainTemplate.Clone())
h.templates[filepath] = template.Must(h.templates[filepath].Parse(content))
}
zap.L().Debug("templates loaded")
return nil
}
func (h *handlers) setDefaultHeaders(w http.ResponseWriter) {
h.push(w, "/css/calc.css")
w.Header().Set("Content-Type", "text/html; charset=utf-8")
}
func (h *handlers) renderError(w http.ResponseWriter, r *http.Request, err error) {
zap.L().Warn("rendering error", zap.Error(err))
// FIXME: log in analytics
if h.opts.Debug {
http.Error(w, fmt.Sprintf("Error: %+v\n", err), http.StatusInternalServerError)
} else {
// FIXME: use error template
http.Error(w, "Internal server error\n", http.StatusInternalServerError)
}
}
func (h *handlers) render(w http.ResponseWriter, r *http.Request, name string, data renderData) {
if h.opts.Debug {
if err := h.loadTemplates(); err != nil {
h.renderError(w, r, err)
return
}
}
tmpl, ok := h.templates[name]
if !ok {
h.renderError(w, r, fmt.Errorf("the template %s does not exist.", name))
return
}
buf := h.bufpool.Get()
defer h.bufpool.Put(buf)
if data == nil {
data = make(renderData)
}
data["req"] = map[string]interface{}{
"name": name,
"r": r,
}
if _, found := data["layout_mode"]; !found {
data["layout_mode"] = "single_column"
}
// set current request in ctxFuncmap objects
h.templatesMutex.Lock()
defer h.templatesMutex.Unlock()
h.funcmap.req = r
if err := tmpl.Execute(buf, data); err != nil {
h.renderError(w, r, err)
return
}
if _, err := buf.WriteTo(w); err != nil {
zap.L().Warn("buf.WriteTo", zap.Error(err))
}
}
func (h *handlers) push(w http.ResponseWriter, resource string) {
pusher, ok := w.(http.Pusher)
if ok {
if err := pusher.Push(resource, nil); err == nil {
zap.L().Warn("failed to push resource", zap.String("path", resource), zap.Error(err))
return
}
}
}