You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.1 KiB
60 lines
1.1 KiB
package root
|
|
|
|
import (
|
|
"embed"
|
|
"html/template"
|
|
"io/fs"
|
|
"log"
|
|
"path/filepath"
|
|
)
|
|
|
|
//go:embed templates static/*
|
|
var webAssetsFS embed.FS
|
|
|
|
var WebTemplates *template.Template
|
|
|
|
// InitializeWebTemplates parses all HTML templates in the embedded filesystem
|
|
func InitializeWebTemplates() error {
|
|
var err error
|
|
WebTemplates, err = parseWebTemplates()
|
|
return err
|
|
}
|
|
|
|
func parseWebTemplates() (*template.Template, error) {
|
|
tmpl := template.New("")
|
|
|
|
err := fs.WalkDir(webAssetsFS, "templates", func(path string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if d.IsDir() {
|
|
return nil
|
|
}
|
|
|
|
if filepath.Ext(path) != ".html" {
|
|
return nil
|
|
}
|
|
|
|
log.Printf("Parsing template: %s", path)
|
|
|
|
content, err := webAssetsFS.ReadFile(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = tmpl.New(filepath.Base(path)).Parse(string(content))
|
|
return err
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return tmpl, nil
|
|
}
|
|
|
|
// GetStaticFS provides access to the static assets filesystem for serving CSS and other static files
|
|
func GetStaticFS() (fs.FS, error) {
|
|
return fs.Sub(webAssetsFS, "static")
|
|
}
|
|
|