Consolidate template loading logic between embedded and host FS
This solves a really annoying bug around my sidebar not loading correctly in prod.
This commit is contained in:
parent
1b85ce0d78
commit
28ade30d2b
3 changed files with 108 additions and 134 deletions
|
|
@ -44,7 +44,7 @@ func newTemplateSystemEmbed() (templateSystemEmbed, error) {
|
|||
}
|
||||
func (ts templateSystemEmbed) loadAll() error {
|
||||
// Then, parse all remaining templates into their named slots, adding the shared stuff
|
||||
page_subdirs := []string{"template/rmo", "template/sync"}
|
||||
page_subdirs := []string{"rmo", "sync"}
|
||||
for _, subdir := range page_subdirs {
|
||||
err := ts.loadTemplateSubdir(subdir)
|
||||
if err != nil {
|
||||
|
|
@ -55,40 +55,21 @@ func (ts templateSystemEmbed) loadAll() error {
|
|||
}
|
||||
|
||||
func (ts templateSystemEmbed) loadTemplateSubdir(subdir string) error {
|
||||
err := fs.WalkDir(ts.sourceFS, subdir, func(path string, d fs.DirEntry, err error) error {
|
||||
template_fs, err := fs.Sub(ts.sourceFS, "template")
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to create template sub-fs: %w", err)
|
||||
}
|
||||
err = fs.WalkDir(template_fs, subdir, func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil || d.IsDir() {
|
||||
return err
|
||||
}
|
||||
|
||||
content, err := fs.ReadFile(ts.sourceFS, path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading template %s: %w", path, err)
|
||||
}
|
||||
|
||||
new_t := template.New(path)
|
||||
addFuncMap(new_t)
|
||||
_, err = new_t.Parse(string(content))
|
||||
new_t, err := parseTemplate(template_fs, path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error parsing '%s': %w", path, err)
|
||||
}
|
||||
short_path := removeLeadingDir(path)
|
||||
shared_subdirs := []string{"template/rmo/component", "template/rmo/layout", "template/sync/component", "template/sync/layout"}
|
||||
for _, shared_subdir := range shared_subdirs {
|
||||
err := ts.addSubdirTemplates(new_t, shared_subdir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to add subdir '%s' templates: %w", shared_subdir, err)
|
||||
}
|
||||
}
|
||||
svg_fs, err := fs.Sub(ts.sourceFS, "template/svg")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get template/svg sub")
|
||||
}
|
||||
err = addSVGTemplates(svg_fs, new_t)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error adding SVG templates: %w", err)
|
||||
}
|
||||
ts.nameToTemplate[short_path] = new_t
|
||||
//log.Debug().Str("path", short_path).Msg("Loaded page template")
|
||||
ts.nameToTemplate[path] = new_t
|
||||
log.Debug().Str("path", path).Msg("Loaded page template")
|
||||
return nil
|
||||
})
|
||||
return err
|
||||
|
|
@ -118,8 +99,7 @@ func (ts templateSystemEmbed) addSubdirTemplates(t *template.Template, subdir st
|
|||
if err != nil {
|
||||
return fmt.Errorf("error adding parse tree '%s': %w", path, err)
|
||||
}
|
||||
//ts.nameToTemplate[short_path] = new_t
|
||||
//log.Debug().Str("path", short_path).Msg("Loaded shared component template")
|
||||
log.Debug().Str("path", short_path).Msg("Loaded shared component template")
|
||||
return nil
|
||||
})
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -2,23 +2,12 @@ package html
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
//"embed"
|
||||
//"errors"
|
||||
"fmt"
|
||||
"html/template"
|
||||
//"io"
|
||||
"io/fs"
|
||||
//"math"
|
||||
"net/http"
|
||||
"os"
|
||||
//"path"
|
||||
//"strconv"
|
||||
//"strings"
|
||||
//"time"
|
||||
|
||||
"github.com/Gleipnir-Technology/nidus-sync/config"
|
||||
//"github.com/aarondl/opt/null"
|
||||
//"github.com/google/uuid"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
|
|
@ -53,31 +42,12 @@ func (ts templateSystemDisk) loadAll() error {
|
|||
return nil
|
||||
}
|
||||
func (ts templateSystemDisk) renderOrError(w http.ResponseWriter, template_name string, context interface{}) {
|
||||
t, err := ts.parseTemplate(template_name)
|
||||
t, err := parseTemplate(ts.sourceFS, template_name)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("template_name", template_name).Msg("Failed to parse template")
|
||||
RespondError(w, "Failed to parse template", err, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
err = ts.addSupportingTemplates(t)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("template_name", template_name).Msg("Failed to add supporting templates")
|
||||
RespondError(w, "Failed to add supporting templates", err, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
svg_fs, err := fs.Sub(ts.sourceFS, "svg")
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("template_name", template_name).Msg("Failed to add get svg subdir")
|
||||
RespondError(w, "Failed to add supporting svg templates", err, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
err = addSVGTemplates(svg_fs, t)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("template_name", template_name).Msg("Failed to add supporting templates")
|
||||
RespondError(w, "Failed to add supporting templates", err, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
err = t.Execute(buf, context)
|
||||
if err != nil {
|
||||
|
|
@ -87,53 +57,6 @@ func (ts templateSystemDisk) renderOrError(w http.ResponseWriter, template_name
|
|||
}
|
||||
buf.WriteTo(w)
|
||||
}
|
||||
func (ts templateSystemDisk) addSupportingTemplates(t *template.Template) error {
|
||||
err := fs.WalkDir(ts.sourceFS, ".", func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if d.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
content, err := fs.ReadFile(ts.sourceFS, path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading template %s: %w", path, err)
|
||||
}
|
||||
|
||||
new_t := template.New(path)
|
||||
addFuncMap(new_t)
|
||||
_, err = new_t.Parse(string(content))
|
||||
if err != nil {
|
||||
return fmt.Errorf("error parsing '%s': %w", path, err)
|
||||
}
|
||||
_, err = t.AddParseTree(new_t.Name(), new_t.Tree)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error adding parse tree '%s': %w", path, err)
|
||||
}
|
||||
//log.Debug().Str("path", path).Msg("Read template")
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("error walking template directory: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (ts templateSystemDisk) parseTemplate(filename string) (*template.Template, error) {
|
||||
t := template.New(filename)
|
||||
log.Debug().Str("filename", filename).Msg("parsing template")
|
||||
addFuncMap(t)
|
||||
content, err := fs.ReadFile(ts.sourceFS, filename)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error reading template %s: %w", filename, err)
|
||||
}
|
||||
_, err = t.Parse(string(content))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing '%s': %w", filename, err)
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
|
||||
/*
|
||||
func addSVGTemplates(fsys fs.FS, templ *template.Template) error {
|
||||
|
|
@ -221,29 +144,3 @@ func (ts templateSystemDisk) parseTemplate(filename string) (*template.Template,
|
|||
return templ, nil
|
||||
}
|
||||
*/
|
||||
func addSVGTemplates(filesystem fs.FS, t *template.Template) error {
|
||||
svgs, err := fs.ReadDir(filesystem, ".")
|
||||
if err != nil {
|
||||
log.Warn().Msg("Failed to read svg directory")
|
||||
return nil
|
||||
}
|
||||
for _, svg := range svgs {
|
||||
content, err := fs.ReadFile(filesystem, svg.Name())
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to read svg '%s' from embedded filesystem: %w", svg, err)
|
||||
}
|
||||
svg_name := svg.Name()
|
||||
svg_template := fmt.Sprintf("{{define \"%s\"}}%s{{end}}", svg_name, string(content))
|
||||
svg_t, err := template.New(svg_name).Parse(svg_template)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to parse svg '%s' from embedded filesystem: %v", svg, err)
|
||||
}
|
||||
_, err = t.AddParseTree(svg_t.Name(), svg_t.Tree)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to add svg '%s' to embedded template: %v", svg, err)
|
||||
}
|
||||
//log.Debug().Str("name", svg_name).Msg("add svg template")
|
||||
}
|
||||
//log.Debug().Msg("Done adding SVG templates")
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
97
html/parse.go
Normal file
97
html/parse.go
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
package html
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/fs"
|
||||
//"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func addSupportingTemplates(sourceFS fs.FS, t *template.Template) error {
|
||||
err := fs.WalkDir(sourceFS, ".", func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if d.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
content, err := fs.ReadFile(sourceFS, path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading template %s: %w", path, err)
|
||||
}
|
||||
|
||||
new_t := template.New(path)
|
||||
addFuncMap(new_t)
|
||||
_, err = new_t.Parse(string(content))
|
||||
if err != nil {
|
||||
return fmt.Errorf("error parsing '%s': %w", path, err)
|
||||
}
|
||||
_, err = t.AddParseTree(new_t.Name(), new_t.Tree)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error adding parse tree '%s': %w", path, err)
|
||||
}
|
||||
//log.Debug().Str("path", path).Msg("Read template")
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("error walking template directory: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func addSVGTemplates(filesystem fs.FS, t *template.Template) error {
|
||||
err := fs.WalkDir(filesystem, "svg", func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil || d.IsDir() {
|
||||
return err
|
||||
}
|
||||
|
||||
content, err := fs.ReadFile(filesystem, path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to read svg '%s' from filesystem: %w", path, err)
|
||||
}
|
||||
svg_name := removeLeadingDir(path)
|
||||
svg_template := fmt.Sprintf("{{define \"%s\"}}%s{{end}}", svg_name, string(content))
|
||||
svg_t, err := template.New(svg_name).Parse(svg_template)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to parse svg '%s' from embedded filesystem: %v", path, err)
|
||||
}
|
||||
_, err = t.AddParseTree(svg_t.Name(), svg_t.Tree)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to add svg '%s' to embedded template: %v", path, err)
|
||||
}
|
||||
//log.Debug().Str("name", svg_name).Msg("add svg template")
|
||||
return nil
|
||||
})
|
||||
//log.Debug().Msg("Done adding SVG templates")
|
||||
return err
|
||||
}
|
||||
func parseTemplate(sourceFS fs.FS, template_name string) (*template.Template, error) {
|
||||
t, err := parseTemplateFile(sourceFS, template_name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to parse template: %w", err)
|
||||
}
|
||||
err = addSupportingTemplates(sourceFS, t)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to add supporting templates: %w", err)
|
||||
}
|
||||
err = addSVGTemplates(sourceFS, t)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to add supporting svg templates: %w", err)
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
func parseTemplateFile(sourceFS fs.FS, filename string) (*template.Template, error) {
|
||||
t := template.New(filename)
|
||||
//log.Debug().Str("filename", filename).Msg("parsing template")
|
||||
addFuncMap(t)
|
||||
content, err := fs.ReadFile(sourceFS, filename)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error reading template %s: %w", filename, err)
|
||||
}
|
||||
_, err = t.Parse(string(content))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing '%s': %w", filename, err)
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue