/
template.go
93 lines (78 loc) · 2.2 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
package template
import (
"bytes"
"context"
"fmt"
"github.com/labstack/echo/v4"
htmlTemplate "html/template"
"net/http"
"strings"
"xorkevin.dev/governor"
)
type (
// Template is a templating service
Template interface {
Execute(templateName string, data interface{}) (string, error)
ExecuteHTML(filename string, data interface{}) (string, error)
}
Service interface {
governor.Service
Template
}
service struct {
t *htmlTemplate.Template
logger governor.Logger
}
)
// New creates a new Template service
func New() Service {
return &service{}
}
func (s *service) Register(r governor.ConfigRegistrar, jr governor.JobRegistrar) {
r.SetDefault("dir", "templates")
}
func (s *service) Init(ctx context.Context, c governor.Config, r governor.ConfigReader, l governor.Logger, g *echo.Group) error {
s.logger = l
l = s.logger.WithData(map[string]string{
"phase": "init",
})
t, err := htmlTemplate.ParseGlob(r.GetStr("dir") + "/*.html")
if err != nil {
if err.Error() == fmt.Sprintf("html/template: pattern matches no files: %#q", r.GetStr("dir")+"/*.html") {
l.Warn("no templates loaded", nil)
t = htmlTemplate.New("default")
} else {
return governor.NewError("Failed to load templates", http.StatusInternalServerError, err)
}
}
s.t = t
if k := t.DefinedTemplates(); k != "" {
l.Info("loaded templates", map[string]string{
"templates": strings.TrimLeft(k, "; "),
})
}
return nil
}
func (s *service) Setup(req governor.ReqSetup) error {
return nil
}
func (s *service) Start(ctx context.Context) error {
return nil
}
func (s *service) Stop(ctx context.Context) {
}
func (s *service) Health() error {
return nil
}
// Execute executes a template and returns the templated string
func (s *service) Execute(templateName string, data interface{}) (string, error) {
b := bytes.Buffer{}
if err := s.t.ExecuteTemplate(&b, templateName, data); err != nil {
return "", governor.NewError("Failed executing template", http.StatusInternalServerError, err)
}
return b.String(), nil
}
// ExecuteHTML executes an html file and returns the templated string
func (s *service) ExecuteHTML(filename string, data interface{}) (string, error) {
return s.Execute(filename+".html", data)
}