-
Notifications
You must be signed in to change notification settings - Fork 2
/
template.go
348 lines (305 loc) · 9.64 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package cypress
import (
"errors"
"html/template"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"
"go.uber.org/zap"
)
var (
//ErrNoFile no file given for Creating a template
ErrNoFile = errors.New("no template file")
//SkinDefault default skin name
SkinDefault = "default"
)
// TemplateConfigFunc shared templates config function
type TemplateConfigFunc func(*template.Template)
// SharedTemplateDetector detector used to check if the given
// path is a shared template or not
type SharedTemplateDetector func(string) bool
// TemplateManager manages the templates by groups and update template group on-demand
// based on the template file update timestamp
type TemplateManager struct {
dir string
lock *sync.RWMutex
shared *template.Template
templates map[string]*template.Template
fileLock *sync.RWMutex
files map[string]time.Time
sharedFiles []string
refresher *time.Ticker
exitChan chan bool
configFunc TemplateConfigFunc
sharedTemplateDetector SharedTemplateDetector
}
// SkinSelector returns a skin name based on the request object
type SkinSelector interface {
GetSkin(request *http.Request) string
}
// SkinSelectorFunc converts a function to SkinSelector interface
type SkinSelectorFunc func(*http.Request) string
// SkinManager a TemplateManager is mapped to a skin, skin manager manages TemplateManagers
// by names.
type SkinManager struct {
defaultSkin *TemplateManager
skins map[string]*TemplateManager
lock *sync.RWMutex
selector SkinSelector
}
// NewTemplateManager creates a template manager for the given dir
func NewTemplateManager(dir, suffix string, refreshInterval time.Duration, configFunc TemplateConfigFunc, sharedTemplateDetector SharedTemplateDetector) *TemplateManager {
dirs := make([]string, 0, 10)
sharedFiles := make([]string, 0, 20)
tmplFiles := make([]string, 0, 20)
filesTime := make(map[string]time.Time)
sharedDetector := sharedTemplateDetector
if sharedDetector == nil {
sharedDetector = func(path string) bool {
return strings.HasPrefix(path, "shared/") || strings.Contains(path, "/shared/")
}
}
// scan dir for all template files
dirs = append(dirs, dir)
for len(dirs) > 0 {
current := dirs[0]
dirs = dirs[1:]
if !strings.HasSuffix(current, "/") {
current = current + "/"
}
zap.L().Info("scan for template files", zap.String("dir", current), zap.String("suffix", suffix))
files, err := os.ReadDir(current)
if err != nil {
zap.L().Error("failed to scan directory for template files", zap.String("dir", current), zap.Error(err))
continue
}
for _, file := range files {
if file.IsDir() {
dirs = append(dirs, current+file.Name()+"/")
} else if strings.HasSuffix(file.Name(), suffix) {
zap.L().Info("template file found", zap.String("file", file.Name()))
path := current + file.Name()
info, err := file.Info()
if err != nil {
zap.L().Error("failed to get file info", zap.String("name", file.Name()), zap.Error(err))
continue
}
filesTime[path] = info.ModTime()
if sharedDetector(path) {
sharedFiles = append(sharedFiles, path)
} else {
tmplFiles = append(tmplFiles, path)
}
}
}
}
shared := template.New("cypress$shared$root")
if configFunc != nil {
configFunc(shared)
}
if len(sharedFiles) > 0 {
t, err := shared.ParseFiles(sharedFiles...)
if err != nil {
zap.L().Error("failed to parse files into shared template, shared will be defaulted to empty", zap.Error(err))
} else {
shared = t
}
}
templates := make(map[string]*template.Template)
for _, file := range tmplFiles {
key := strings.Trim(strings.TrimPrefix(strings.TrimSuffix(file, filepath.Ext(file)), dir), "/\\")
tmpl, err := shared.Clone()
if err != nil {
zap.L().Error("failed to clone shared template", zap.Error(err), zap.String("file", file))
} else {
tmpl, err = tmpl.ParseFiles(file)
if err != nil {
zap.L().Error("failed to parse template file", zap.Error(err), zap.String("file", file))
} else {
templates[key] = tmpl
}
}
}
mgr := &TemplateManager{
dir: dir,
lock: &sync.RWMutex{},
shared: shared,
templates: templates,
fileLock: &sync.RWMutex{},
files: filesTime,
sharedFiles: sharedFiles,
refresher: time.NewTicker(refreshInterval),
exitChan: make(chan bool),
configFunc: configFunc,
sharedTemplateDetector: sharedDetector,
}
go func() {
for {
select {
case <-mgr.refresher.C:
mgr.refreshTemplates()
case <-mgr.exitChan:
return
}
}
}()
return mgr
}
// Close closes the template manager and release all resources
func (manager *TemplateManager) Close() {
manager.exitChan <- true
manager.refresher.Stop()
close(manager.exitChan)
}
// GetTemplate retrieve a template with the specified name from
// template manager, this returns the template and a boolean value
// to indicate if the template is found or not
func (manager *TemplateManager) GetTemplate(name string) (*template.Template, bool) {
manager.lock.RLock()
defer manager.lock.RUnlock()
result, ok := manager.templates[name]
return result, ok
}
func (manager *TemplateManager) refreshTemplates() {
files := make([]string, 0, len(manager.files))
func() {
manager.fileLock.RLock()
defer manager.fileLock.RUnlock()
for key := range manager.files {
files = append(files, key)
}
}()
reparseAll := false
filesToReparse := make([]string, 0, 20)
for _, file := range files {
var t time.Time
var ok bool
stat, err := os.Stat(file)
if err != nil {
zap.L().Error("failed to get file meta info", zap.Error(err), zap.String("file", file))
continue
}
func() {
manager.fileLock.RLock()
defer manager.fileLock.RUnlock()
t, ok = manager.files[file]
}()
if !ok {
zap.L().Error("failed to see the file time", zap.String("file", file))
continue
}
if t.Before(stat.ModTime()) {
func() {
manager.fileLock.Lock()
defer manager.fileLock.Unlock()
manager.files[file] = stat.ModTime()
}()
if manager.sharedTemplateDetector(file) {
// a shared template file has been changed, so we need to re-parse
// all templates, let the loop continue to allow detect and update
// last updated time for all files
reparseAll = true
} else {
// only the given template need to be updated
filesToReparse = append(filesToReparse, file)
}
}
}
if reparseAll && len(manager.sharedFiles) > 0 {
shared := template.New("cypress$shared$root")
if manager.configFunc != nil {
manager.configFunc(shared)
}
t, err := shared.ParseFiles(manager.sharedFiles...)
if err != nil {
manager.shared = shared
zap.L().Error("failed to parse files into shared template, shared will be defaulted to empty", zap.Error(err))
} else {
manager.shared = t
}
filesToReparse = files
}
for _, file := range filesToReparse {
if !manager.sharedTemplateDetector(file) {
name := strings.Trim(strings.TrimPrefix(strings.TrimSuffix(file, filepath.Ext(file)), manager.dir), "/\\")
tmpl, err := manager.shared.Clone()
if err != nil {
zap.L().Error("failed to clone shared template", zap.Error(err), zap.String("file", file))
} else {
tmpl, err = tmpl.ParseFiles(file)
if err != nil {
zap.L().Error("failed to parse template file", zap.Error(err), zap.String("file", file))
} else {
func() {
manager.lock.Lock()
defer manager.lock.Unlock()
manager.templates[name] = tmpl
}()
zap.L().Info("template file reparsed", zap.String("file", file))
}
}
}
}
}
// NewSkinManager creates a skin manager object
func NewSkinManager(defaultSkin *TemplateManager) *SkinManager {
return &SkinManager{defaultSkin, make(map[string]*TemplateManager), &sync.RWMutex{}, nil}
}
// AddSkin adds a skin
func (skinMgr *SkinManager) AddSkin(name string, tmplMgr *TemplateManager) {
skinMgr.lock.Lock()
defer skinMgr.lock.Unlock()
skinMgr.skins[name] = tmplMgr
}
// RemoveSkin removes a skin
func (skinMgr *SkinManager) RemoveSkin(name string) {
skinMgr.lock.Lock()
defer skinMgr.lock.Unlock()
delete(skinMgr.skins, name)
}
// GetDefaultSkin gets the default skin
func (skinMgr *SkinManager) GetDefaultSkin() *TemplateManager {
skinMgr.lock.RLock()
defer skinMgr.lock.RUnlock()
return skinMgr.defaultSkin
}
// GetSkinOrDefault gets the skin with the given name if it's not
// found return the default skin
func (skinMgr *SkinManager) GetSkinOrDefault(name string) *TemplateManager {
skinMgr.lock.RLock()
defer skinMgr.lock.RUnlock()
tmplMgr, ok := skinMgr.skins[name]
if !ok {
return skinMgr.defaultSkin
}
return tmplMgr
}
// GetSkin find skin by name
func (skinMgr *SkinManager) GetSkin(name string) (*TemplateManager, bool) {
skinMgr.lock.RLock()
defer skinMgr.lock.RUnlock()
tmplMgr, ok := skinMgr.skins[name]
return tmplMgr, ok
}
// ApplySelector find skin by applying selector, if selector is not configured
// default skin will be returned, if the skin name returned by the selector cannot
// be found, this returns nil
func (skinMgr *SkinManager) ApplySelector(request *http.Request) (*TemplateManager, string) {
if skinMgr.selector == nil {
return skinMgr.GetDefaultSkin(), SkinDefault
}
skinMgr.lock.RLock()
defer skinMgr.lock.RUnlock()
name := skinMgr.selector.GetSkin(request)
skin, _ := skinMgr.GetSkin(name)
return skin, name
}
// WithSelector sets the skin selector for the skin manager
func (skinMgr *SkinManager) WithSelector(selector SkinSelector) {
skinMgr.lock.Lock()
defer skinMgr.lock.Unlock()
skinMgr.selector = selector
}