-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtemplate.go
115 lines (103 loc) · 2.71 KB
/
template.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
package webserver
import (
"fmt"
"html/template"
"net/url"
"path/filepath"
"strings"
"github.com/sirupsen/logrus"
)
type pageTemplate struct {
preloads []string
template *template.Template
}
type templates struct {
templates map[string]pageTemplate
folder string
exec func(string, interface{}) (string, error)
addErr error
}
func newTemplates(folder string, reload bool) *templates {
t := &templates{
templates: make(map[string]pageTemplate),
folder: folder,
}
t.exec = t.execTemplateToString
if reload {
t.exec = t.exectWithReload
}
return t
}
// filepath constructs the template path from the template ID
func (t *templates) filepath(name string) string {
return filepath.Join(t.folder, name+".tmpl")
}
func (t *templates) addTemplate(name string, preloads ...string) *templates {
if t.addErr != nil {
return t
}
files := make([]string, 0, len(preloads)+1)
for i := range preloads {
files = append(files, preloads[i])
}
files = append(files, t.filepath(name))
temp, err := template.New(name).Funcs(templateFuncs).ParseFiles(files...)
if err != nil {
t.addErr = fmt.Errorf("error adding template %s: %w", name, err)
return t
}
t.templates[name] = pageTemplate{
preloads: preloads,
template: temp,
}
return t
}
// buildErr returns any error encountered during addTemplate the error is cleared
func (t *templates) buildErr() error {
err := t.addErr
t.addErr = nil
return err
}
func (t *templates) reloadTemplates() error {
var errorStrings []string
for name, tmpl := range t.templates {
t.addTemplate(name, tmpl.preloads...)
if t.buildErr() != nil {
logrus.Errorf(t.buildErr().Error())
}
}
if errorStrings == nil {
return nil
}
return fmt.Errorf(strings.Join(errorStrings, " | "))
}
func (t *templates) execTemplateToString(name string, data interface{}) (string, error) {
temp, ok := t.templates[name]
if !ok {
return "", fmt.Errorf("template %s unknown", name)
}
var page strings.Builder
err := temp.template.ExecuteTemplate(&page, name, data)
return page.String(), err
}
// exectWithReload is the same as execTemplateToString but will reload the template first
func (t *templates) exectWithReload(name string, data interface{}) (string, error) {
tmpl, found := t.templates[name]
if !found {
return "", fmt.Errorf("templates %s nout found", name)
}
t.addTemplate(name, tmpl.preloads...)
logrus.Debugf("reloaded HTML template %q\n", name)
return t.execTemplateToString(name, data)
}
var templateFuncs = template.FuncMap{
"toUpper": strings.ToUpper,
// urlBase attempts to get the domain name without TLD
"urlBase": func(uri string) string {
u, err := url.Parse(uri)
if err != nil {
logrus.Errorf("failed to parse URL: %s", uri)
}
return u.Host
},
}