-
Notifications
You must be signed in to change notification settings - Fork 1
/
templater.go
100 lines (91 loc) · 2.45 KB
/
templater.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
package gotemplater
import (
"bytes"
"html/template"
"io"
"log"
"os"
"sync"
)
//Templater instance to perform hot reload on Go instance
type Templater struct {
logger *log.Logger
template *template.Template
builder *templaterBuilder
watcher *templaterWatcher
hotReload bool
outputDir string
bufferPool *sync.Pool
funcMap template.FuncMap
}
func (t *Templater) Run() {
t.builder.initBuild()
t.reload()
if t.hotReload {
t.watcher.Run()
t.logger.Println("Templater started")
}
}
func (t *Templater) Stop() {
if t.hotReload {
t.watcher.Stop()
t.logger.Println("Templater stopped")
}
}
func (t *Templater) reload() {
newTemplate, err := template.New("").
Funcs(t.funcMap).
ParseGlob(t.outputDir + "/*")
if err != nil {
log.Println(err.Error())
return
}
t.template = newTemplate
t.logger.Println("Templater reloaded")
}
func (t *Templater) Render(wr io.Writer, name string, data interface{}) error {
return t.template.ExecuteTemplate(wr, name, data)
}
func (t *Templater) RenderToByteArray(name string, data interface{}) ([]byte, error) {
buffer := t.bufferPool.Get().(*bytes.Buffer)
if err := t.Render(buffer, name, data); err != nil {
return nil, err
}
defer buffer.Reset()
resBuffer := make([]byte, buffer.Len())
copy(resBuffer, buffer.Bytes())
defer t.bufferPool.Put(buffer)
return resBuffer, nil
}
func (t *Templater) RenderToString(name string, data interface{}) (string, error) {
buffer := t.bufferPool.Get().(*bytes.Buffer)
if err := t.Render(buffer, name, data); err != nil {
return "", err
}
defer buffer.Reset()
defer t.bufferPool.Put(buffer)
return buffer.String(), nil
}
//NewTemplater will return a new and ready to use Templater instance
func NewTemplater(conf *Config) *Templater {
instance := new(Templater)
instance.outputDir = conf.OutputDir
instance.hotReload = conf.AutoReload
instance.logger = log.New(os.Stdout, "[GO-TEMPLATER] ", log.Ltime)
instance.builder = newTemplaterBuilder(conf.InputDir, conf.OutputDir, instance.logger)
instance.watcher = newTemplaterWatcher(conf.InputDir, instance.logger)
instance.funcMap = conf.FuncMap
instance.bufferPool = new(sync.Pool)
instance.bufferPool.New = func() interface{} {
byteBuffer := make([]byte, 0)
return bytes.NewBuffer(byteBuffer)
}
if conf.AutoReload {
rebuildEv := func(data string) {
instance.reload()
}
instance.watcher.addListener(instance.builder.generate)
instance.watcher.addListener(rebuildEv)
}
return instance
}