Merge branch 'develop' into main

This commit is contained in:
2022-01-04 22:32:30 +01:00
4 changed files with 79 additions and 30 deletions
+7 -1
View File
@@ -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,12 @@ func main() {
logrus.Fatal(err)
}
templateFuncs := &templates.Funcs{Version: Version}
err = templates.Load(templateFuncs)
if err != nil {
logrus.Fatal(err)
}
ghostClient := &httpclient.HTTPClient{
Addr: config.Content.Backend.Addr,
Secured: config.Content.Backend.Secured,
+1 -1
View File
@@ -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)
}
+57 -15
View File
@@ -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 {
return template.FuncMap{
"add": func(i int) int {
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
},
"sub": func(i int) int {
}
func (f *Funcs) sub(i int) int {
return i - 1
},
"getJSAppURL": func() string {
return URLJSApp
},
"getIndexURL": func() string {
}
func (f *Funcs) getJSAppURL() string {
f.initOnce.Do(f.init)
return f.compiledJSAppURL
}
func (f *Funcs) getIndexURL() string {
return URLIndex
},
"getBlogURL": func() string {
}
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": f.add,
"sub": f.sub,
"getJSAppURL": f.getJSAppURL,
"getIndexURL": f.getIndexURL,
"getBlogURL": f.getBlogURL,
}
}
+11 -10
View File
@@ -21,17 +21,16 @@ import (
var content embed.FS
// List of compiled go-templates
var Templates *template.Template
var Templates *template.Template = template.New("")
// Load embeded templates
func init() {
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 {
panic(err)
return fmt.Errorf("cannot match templates names using glob, %w", err)
}
buf := bytes.NewBuffer(nil)
@@ -42,21 +41,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
@@ -70,8 +71,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()