refactoring. remove global state, use new logging, config functions
This commit is contained in:
@@ -3,24 +3,24 @@ package templates
|
||||
import (
|
||||
"html/template"
|
||||
"time"
|
||||
|
||||
"fornaxian.com/pixeldrain-web/conf"
|
||||
)
|
||||
|
||||
var funcMap = template.FuncMap{
|
||||
"bgPatternCount": bgPatternCount,
|
||||
"debugMode": debugMode,
|
||||
"apiUrl": apiURL,
|
||||
func (tm *TemplateManager) funcMap() template.FuncMap {
|
||||
return template.FuncMap{
|
||||
"bgPatternCount": tm.bgPatternCount,
|
||||
"debugMode": tm.debugMode,
|
||||
"apiUrl": tm.apiURL,
|
||||
}
|
||||
}
|
||||
|
||||
func bgPatternCount() uint8 {
|
||||
func (tm *TemplateManager) bgPatternCount() uint8 {
|
||||
return uint8(time.Now().UnixNano() % 17)
|
||||
}
|
||||
|
||||
func debugMode() bool {
|
||||
return conf.DebugMode()
|
||||
func (tm *TemplateManager) debugMode() bool {
|
||||
return tm.debugModeEnabled
|
||||
}
|
||||
|
||||
func apiURL() string {
|
||||
return conf.ApiUrlExternal()
|
||||
func (tm *TemplateManager) apiURL() string {
|
||||
return tm.externalAPIEndpoint
|
||||
}
|
||||
|
61
webcontroller/templates/manager.go
Normal file
61
webcontroller/templates/manager.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package templates
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/Fornaxian/log"
|
||||
)
|
||||
|
||||
type TemplateManager struct {
|
||||
templates *template.Template
|
||||
|
||||
// Config
|
||||
templateDir string
|
||||
externalAPIEndpoint string
|
||||
debugModeEnabled bool
|
||||
}
|
||||
|
||||
func New(templateDir, externalAPIEndpoint string, debugMode bool) *TemplateManager {
|
||||
return &TemplateManager{
|
||||
templateDir: templateDir,
|
||||
externalAPIEndpoint: externalAPIEndpoint,
|
||||
debugModeEnabled: debugMode,
|
||||
}
|
||||
}
|
||||
|
||||
// ParseTemplates parses the templates in the template directory which is
|
||||
// defined in the config file
|
||||
func (tm *TemplateManager) ParseTemplates() {
|
||||
var templatePaths []string
|
||||
|
||||
filepath.Walk(tm.templateDir, func(path string, f os.FileInfo, err error) error {
|
||||
if f.IsDir() {
|
||||
return nil
|
||||
}
|
||||
templatePaths = append(templatePaths, path)
|
||||
log.Info("Template found: %s", path)
|
||||
return nil
|
||||
})
|
||||
|
||||
tpl := template.New("")
|
||||
|
||||
// Import template functions from funcs.go
|
||||
tpl = tpl.Funcs(tm.funcMap())
|
||||
|
||||
var err error
|
||||
tpl, err = tpl.ParseFiles(templatePaths...)
|
||||
if err != nil {
|
||||
log.Error("Template parsing failed: %v", err)
|
||||
}
|
||||
|
||||
// Swap out the old templates with the new templates, to minimize
|
||||
// modifications to the original variable.
|
||||
tm.templates = tpl
|
||||
}
|
||||
|
||||
// Get returns the templates, so they can be used to render views
|
||||
func (tm *TemplateManager) Get() *template.Template {
|
||||
return tm.templates
|
||||
}
|
@@ -1,46 +0,0 @@
|
||||
package templates
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"fornaxian.com/pixeldrain-web/conf"
|
||||
"fornaxian.com/pixeldrain-web/log"
|
||||
)
|
||||
|
||||
var templates *template.Template
|
||||
var templatePaths []string
|
||||
|
||||
// ParseTemplates parses the templates in the template directory which is
|
||||
// defined in the config file
|
||||
func ParseTemplates() {
|
||||
filepath.Walk(conf.TemplateDir(), func(path string, f os.FileInfo, err error) error {
|
||||
if f.IsDir() {
|
||||
return nil
|
||||
}
|
||||
templatePaths = append(templatePaths, path)
|
||||
log.Info("Template found: %s", path)
|
||||
return nil
|
||||
})
|
||||
|
||||
tpl := template.New("")
|
||||
|
||||
// Import template functions from funcs.go
|
||||
tpl = tpl.Funcs(funcMap)
|
||||
|
||||
var err error
|
||||
tpl, err = tpl.ParseFiles(templatePaths...)
|
||||
if err != nil {
|
||||
log.Error("Template parsing failed: %v", err)
|
||||
}
|
||||
|
||||
// Swap out the old templates with the new templates, to minimize
|
||||
// modifications to the original variable.
|
||||
templates = tpl
|
||||
}
|
||||
|
||||
// Get returns the templates, so they can be used to render views
|
||||
func Get() *template.Template {
|
||||
return templates
|
||||
}
|
Reference in New Issue
Block a user