From 277686086ea2eccd00fd66727d8a42e6132ebe6c Mon Sep 17 00:00:00 2001 From: Nikita Tokarchuk Date: Tue, 4 Jan 2022 22:29:53 +0100 Subject: [PATCH 1/3] Use constructor-like function to load templates --- cmd/renderer/main.go | 7 ++++++- pkg/templates/templates.go | 12 +++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/cmd/renderer/main.go b/cmd/renderer/main.go index 43e93e7..854d923 100644 --- a/cmd/renderer/main.go +++ b/cmd/renderer/main.go @@ -12,7 +12,7 @@ import ( "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/config" "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/ghost/v4api/httpclient" "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/routes" - _ "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/templates" + "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/templates" ) var Version = "nightly" @@ -52,6 +52,11 @@ func main() { logrus.Fatal(err) } + err = templates.Load() + if err != nil { + logrus.Fatal(err) + } + ghostClient := &httpclient.HTTPClient{ Addr: config.Content.Backend.Addr, Secured: config.Content.Backend.Secured, diff --git a/pkg/templates/templates.go b/pkg/templates/templates.go index 9713638..eb12030 100644 --- a/pkg/templates/templates.go +++ b/pkg/templates/templates.go @@ -24,14 +24,14 @@ var content embed.FS var Templates *template.Template // Load embeded templates -func init() { +func Load() (err error) { Templates = template.New("") Templates.Funcs(UseFuncs()) tmplNames, err := fs.Glob(content, "*.go.tmpl") if err != nil { - panic(err) + return fmt.Errorf("cannot match templates names using glob, %w", err) } buf := bytes.NewBuffer(nil) @@ -42,21 +42,23 @@ func init() { tmplContent, err := content.Open(name) if err != nil { - panic(err) + return fmt.Errorf("cannot open template content, name:%s, %w", name, err) } size, err := buf.ReadFrom(tmplContent) if err != nil { - panic(err) + return fmt.Errorf("cannot read template content, name:%s, %w", name, err) } tmpl, err := Templates.New(name).Parse(buf.String()) if err != nil { - panic(err) + return fmt.Errorf("cannot parse template, name:%s, %w", name, err) } logrus.Debugf("Found template: %s, size:%d", tmpl.Name(), size) } logrus.Debugf("Templates loading complete%s", Templates.DefinedTemplates()) + + return } // MustLookup wraps lookup function for the root template namespace From 8e87eb0adf1947b468df979e66ce4cae5db557d8 Mon Sep 17 00:00:00 2001 From: Nikita Tokarchuk Date: Tue, 4 Jan 2022 22:30:54 +0100 Subject: [PATCH 2/3] Funcs that panic on purpose should be named properly --- pkg/routes/output.go | 2 +- pkg/templates/templates.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/routes/output.go b/pkg/routes/output.go index 4e61b25..567c255 100644 --- a/pkg/routes/output.go +++ b/pkg/routes/output.go @@ -25,7 +25,7 @@ func (tw *TemplateWriter) SetHeader(rh *fasthttp.ResponseHeader) { // Write executes the template and writes result to the response writer func (tw *TemplateWriter) Write(w io.Writer, content interface{}) error { - template := templates.GetTemplateOf(content) + template := templates.MustGetTemplateOf(content) return template.Execute(w, content) } diff --git a/pkg/templates/templates.go b/pkg/templates/templates.go index eb12030..9cece2c 100644 --- a/pkg/templates/templates.go +++ b/pkg/templates/templates.go @@ -72,8 +72,8 @@ func MustLookup(name string) *template.Template { return tmpl } -// GetTemplateOf returns template which is mapped to the content data -func GetTemplateOf(content interface{}) (template *template.Template) { +// MustGetTemplateOf returns template which is mapped to the content data +func MustGetTemplateOf(content interface{}) (template *template.Template) { el := reflect.TypeOf(content) numField := el.NumField() From 3f10f7b912bb2b46980e0d07a57c1842d30b4392 Mon Sep 17 00:00:00 2001 From: Nikita Tokarchuk Date: Tue, 4 Jan 2022 22:32:06 +0100 Subject: [PATCH 3/3] Implement template funcs controller instead of simple funcs map --- cmd/renderer/main.go | 3 +- pkg/templates/funcs.go | 78 +++++++++++++++++++++++++++++--------- pkg/templates/templates.go | 7 ++-- 3 files changed, 65 insertions(+), 23 deletions(-) diff --git a/cmd/renderer/main.go b/cmd/renderer/main.go index 854d923..46419a7 100644 --- a/cmd/renderer/main.go +++ b/cmd/renderer/main.go @@ -52,7 +52,8 @@ func main() { logrus.Fatal(err) } - err = templates.Load() + templateFuncs := &templates.Funcs{Version: Version} + err = templates.Load(templateFuncs) if err != nil { logrus.Fatal(err) } diff --git a/pkg/templates/funcs.go b/pkg/templates/funcs.go index aedbf17..41d1927 100644 --- a/pkg/templates/funcs.go +++ b/pkg/templates/funcs.go @@ -1,24 +1,66 @@ package templates -import "html/template" +import ( + "html/template" + "net/url" + "sync" +) -// UseFuncs returns a func map with template helpers functions -func UseFuncs() template.FuncMap { +type Funcs struct { + Version string + + compiledJSAppURL string + + initOnce sync.Once +} + +func (f *Funcs) init() { + + jsAppURL, err := url.Parse(URLJSApp) + if err != nil { + panic(err) + } + + { + q := jsAppURL.Query() + q.Add("version", f.Version) + + jsAppURL.RawQuery = q.Encode() + } + + f.compiledJSAppURL = jsAppURL.String() +} + +func (f *Funcs) add(i int) int { + return i + 1 +} + +func (f *Funcs) sub(i int) int { + return i - 1 +} + +func (f *Funcs) getJSAppURL() string { + + f.initOnce.Do(f.init) + + return f.compiledJSAppURL +} + +func (f *Funcs) getIndexURL() string { + return URLIndex +} + +func (f *Funcs) getBlogURL() string { + return URLBlog +} + +// Use returns a func map with template helpers functions +func (f *Funcs) Use() template.FuncMap { return template.FuncMap{ - "add": func(i int) int { - return i + 1 - }, - "sub": func(i int) int { - return i - 1 - }, - "getJSAppURL": func() string { - return URLJSApp - }, - "getIndexURL": func() string { - return URLIndex - }, - "getBlogURL": func() string { - return URLBlog - }, + "add": f.add, + "sub": f.sub, + "getJSAppURL": f.getJSAppURL, + "getIndexURL": f.getIndexURL, + "getBlogURL": f.getBlogURL, } } diff --git a/pkg/templates/templates.go b/pkg/templates/templates.go index 9cece2c..90c27d6 100644 --- a/pkg/templates/templates.go +++ b/pkg/templates/templates.go @@ -21,13 +21,12 @@ import ( var content embed.FS // List of compiled go-templates -var Templates *template.Template +var Templates *template.Template = template.New("") // Load embeded templates -func Load() (err error) { +func Load(funcs *Funcs) (err error) { - Templates = template.New("") - Templates.Funcs(UseFuncs()) + Templates.Funcs(funcs.Use()) tmplNames, err := fs.Glob(content, "*.go.tmpl") if err != nil {