Merge branch 'develop' into main

develop
Nikita Tokarchuk 3 years ago
commit 93edc10fda
Signed by: mainnika
GPG Key ID: A595FB7E3E56911C
  1. 8
      cmd/renderer/main.go
  2. 2
      pkg/routes/output.go
  3. 72
      pkg/templates/funcs.go
  4. 21
      pkg/templates/templates.go

@ -12,7 +12,7 @@ import (
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/config" "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/ghost/v4api/httpclient"
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/routes" "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" var Version = "nightly"
@ -52,6 +52,12 @@ func main() {
logrus.Fatal(err) logrus.Fatal(err)
} }
templateFuncs := &templates.Funcs{Version: Version}
err = templates.Load(templateFuncs)
if err != nil {
logrus.Fatal(err)
}
ghostClient := &httpclient.HTTPClient{ ghostClient := &httpclient.HTTPClient{
Addr: config.Content.Backend.Addr, Addr: config.Content.Backend.Addr,
Secured: config.Content.Backend.Secured, Secured: config.Content.Backend.Secured,

@ -25,7 +25,7 @@ func (tw *TemplateWriter) SetHeader(rh *fasthttp.ResponseHeader) {
// Write executes the template and writes result to the response writer // Write executes the template and writes result to the response writer
func (tw *TemplateWriter) Write(w io.Writer, content interface{}) error { func (tw *TemplateWriter) Write(w io.Writer, content interface{}) error {
template := templates.GetTemplateOf(content) template := templates.MustGetTemplateOf(content)
return template.Execute(w, content) return template.Execute(w, content)
} }

@ -1,24 +1,66 @@
package templates package templates
import "html/template" import (
"html/template"
"net/url"
"sync"
)
// UseFuncs returns a func map with template helpers functions type Funcs struct {
func UseFuncs() template.FuncMap { Version string
return template.FuncMap{
"add": func(i int) int { 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 return i + 1
}, }
"sub": func(i int) int {
func (f *Funcs) sub(i int) int {
return i - 1 return i - 1
}, }
"getJSAppURL": func() string {
return URLJSApp func (f *Funcs) getJSAppURL() string {
},
"getIndexURL": func() string { f.initOnce.Do(f.init)
return f.compiledJSAppURL
}
func (f *Funcs) getIndexURL() string {
return URLIndex return URLIndex
}, }
"getBlogURL": func() string {
func (f *Funcs) getBlogURL() string {
return URLBlog 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,
} }
} }

@ -21,17 +21,16 @@ import (
var content embed.FS var content embed.FS
// List of compiled go-templates // List of compiled go-templates
var Templates *template.Template var Templates *template.Template = template.New("")
// Load embeded templates // Load embeded templates
func init() { func Load(funcs *Funcs) (err error) {
Templates = template.New("") Templates.Funcs(funcs.Use())
Templates.Funcs(UseFuncs())
tmplNames, err := fs.Glob(content, "*.go.tmpl") tmplNames, err := fs.Glob(content, "*.go.tmpl")
if err != nil { if err != nil {
panic(err) return fmt.Errorf("cannot match templates names using glob, %w", err)
} }
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
@ -42,21 +41,23 @@ func init() {
tmplContent, err := content.Open(name) tmplContent, err := content.Open(name)
if err != nil { if err != nil {
panic(err) return fmt.Errorf("cannot open template content, name:%s, %w", name, err)
} }
size, err := buf.ReadFrom(tmplContent) size, err := buf.ReadFrom(tmplContent)
if err != nil { 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()) tmpl, err := Templates.New(name).Parse(buf.String())
if err != nil { 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("Found template: %s, size:%d", tmpl.Name(), size)
} }
logrus.Debugf("Templates loading complete%s", Templates.DefinedTemplates()) logrus.Debugf("Templates loading complete%s", Templates.DefinedTemplates())
return
} }
// MustLookup wraps lookup function for the root template namespace // MustLookup wraps lookup function for the root template namespace
@ -70,8 +71,8 @@ func MustLookup(name string) *template.Template {
return tmpl return tmpl
} }
// GetTemplateOf returns template which is mapped to the content data // MustGetTemplateOf returns template which is mapped to the content data
func GetTemplateOf(content interface{}) (template *template.Template) { func MustGetTemplateOf(content interface{}) (template *template.Template) {
el := reflect.TypeOf(content) el := reflect.TypeOf(content)
numField := el.NumField() numField := el.NumField()

Loading…
Cancel
Save