mirror of
https://github.com/Kugelschieber/schnittfest.git
synced 2026-01-18 10:20:27 +00:00
Added option to hot reload templates for development.
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
|||||||
"github.com/emvi/logbuch"
|
"github.com/emvi/logbuch"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -18,7 +19,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func LoadTemplates() {
|
func LoadTemplates() {
|
||||||
tplCache = util.NewTemplateCache()
|
tplCache = util.NewTemplateCache(strings.ToLower(os.Getenv("SCHNITTFEST_HOT_RELOAD")) == "true")
|
||||||
templateBase := os.Getenv("SCHNITTFEST_TEMPLATE_BASE")
|
templateBase := os.Getenv("SCHNITTFEST_TEMPLATE_BASE")
|
||||||
|
|
||||||
if templateBase == "" {
|
if templateBase == "" {
|
||||||
|
|||||||
@@ -3,5 +3,6 @@ set SCHNITTFEST_LOGLEVEL=debug
|
|||||||
set SCHNITTFEST_ALLOWED_ORIGINS=*
|
set SCHNITTFEST_ALLOWED_ORIGINS=*
|
||||||
set SCHNITTFEST_TLS_ENABLE=false
|
set SCHNITTFEST_TLS_ENABLE=false
|
||||||
set SCHNITTFEST_DISABLE_CACHE=true
|
set SCHNITTFEST_DISABLE_CACHE=true
|
||||||
|
set SCHNITTFEST_HOT_RELOAD=true
|
||||||
|
|
||||||
go run main.go
|
go run main.go
|
||||||
|
|||||||
@@ -5,5 +5,6 @@ export SCHNITTFEST_LOGLEVEL=debug
|
|||||||
export SCHNITTFEST_ALLOWED_ORIGINS=*
|
export SCHNITTFEST_ALLOWED_ORIGINS=*
|
||||||
export SCHNITTFEST_TLS_ENABLE=false
|
export SCHNITTFEST_TLS_ENABLE=false
|
||||||
export SCHNITTFEST_DISABLE_CACHE=true
|
export SCHNITTFEST_DISABLE_CACHE=true
|
||||||
|
export SCHNITTFEST_HOT_RELOAD=true
|
||||||
|
|
||||||
go run main.go
|
go run main.go
|
||||||
|
|||||||
@@ -6,23 +6,30 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type cachedTemplate struct {
|
||||||
|
tpl *template.Template
|
||||||
|
files []string
|
||||||
|
}
|
||||||
|
|
||||||
// TemplateCache caches templates.
|
// TemplateCache caches templates.
|
||||||
type TemplateCache struct {
|
type TemplateCache struct {
|
||||||
templates map[string]*template.Template
|
templates map[string]cachedTemplate
|
||||||
|
disabled bool
|
||||||
mutex sync.RWMutex
|
mutex sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTemplateCache creates a new template cache.
|
// NewTemplateCache creates a new template cache.
|
||||||
func NewTemplateCache() *TemplateCache {
|
// If disabled is set to true, the templates are reloaded on each call.
|
||||||
return &TemplateCache{templates: make(map[string]*template.Template)}
|
func NewTemplateCache(disabled bool) *TemplateCache {
|
||||||
|
return &TemplateCache{templates: make(map[string]cachedTemplate), disabled: disabled}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseFiles parses the given template files and stores them as one template called name.
|
// ParseFiles parses the given template files and stores them as one template called name.
|
||||||
func (tplcache *TemplateCache) ParseFiles(name string, files ...string) (*template.Template, error) {
|
func (tplcache *TemplateCache) ParseFiles(name string, files ...string) (*template.Template, error) {
|
||||||
if tplcache.templates[name] != nil {
|
if _, ok := tplcache.templates[name]; ok && !tplcache.disabled {
|
||||||
tplcache.mutex.RLock()
|
tplcache.mutex.RLock()
|
||||||
defer tplcache.mutex.RUnlock()
|
defer tplcache.mutex.RUnlock()
|
||||||
return tplcache.templates[name], nil
|
return tplcache.templates[name].tpl, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
tplcache.mutex.Lock()
|
tplcache.mutex.Lock()
|
||||||
@@ -34,16 +41,27 @@ func (tplcache *TemplateCache) ParseFiles(name string, files ...string) (*templa
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
tplcache.templates[name] = tpl
|
tplcache.templates[name] = cachedTemplate{tpl, files}
|
||||||
return tpl, nil
|
return tpl, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetTemplate returns a cached template by name or nil if not found.
|
// GetTemplate returns a cached template by name or nil if not found.
|
||||||
func (tplcache *TemplateCache) GetTemplate(name string) *template.Template {
|
func (tplcache *TemplateCache) GetTemplate(name string) *template.Template {
|
||||||
if tplcache.templates[name] != nil {
|
tpl, ok := tplcache.templates[name]
|
||||||
|
|
||||||
|
if ok && tplcache.disabled {
|
||||||
|
newTpl, err := tplcache.ParseFiles(name, tpl.files...)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
logbuch.Error("Error parsing file on cache rebuild", logbuch.Fields{"err": err, "name": name})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return newTpl
|
||||||
|
} else if ok {
|
||||||
tplcache.mutex.RLock()
|
tplcache.mutex.RLock()
|
||||||
defer tplcache.mutex.RUnlock()
|
defer tplcache.mutex.RUnlock()
|
||||||
return tplcache.templates[name]
|
return tplcache.templates[name].tpl
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -51,5 +69,15 @@ func (tplcache *TemplateCache) GetTemplate(name string) *template.Template {
|
|||||||
|
|
||||||
// Clear clears the template cache.
|
// Clear clears the template cache.
|
||||||
func (tplcache *TemplateCache) Clear() {
|
func (tplcache *TemplateCache) Clear() {
|
||||||
tplcache.templates = make(map[string]*template.Template)
|
tplcache.templates = make(map[string]cachedTemplate)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enable enables the cache, so that each template is loaded from cache.
|
||||||
|
func (tplcache *TemplateCache) Enable() {
|
||||||
|
tplcache.disabled = false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Disable disables the cache, so that each template is reloaded on each call.
|
||||||
|
func (tplcache *TemplateCache) Disable() {
|
||||||
|
tplcache.disabled = true
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user