63 lines
1,009 B
Go
63 lines
1,009 B
Go
package frontend
|
|
|
|
import (
|
|
"embed"
|
|
"html/template"
|
|
"io"
|
|
"io/fs"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
var (
|
|
//go:embed static
|
|
static embed.FS
|
|
Static fs.FS
|
|
|
|
//go:embed *.html
|
|
templatesFS embed.FS
|
|
Templates *template.Template
|
|
|
|
funcs = template.FuncMap{
|
|
"static": staticFn,
|
|
"staticCSS": func(filename string) (template.CSS, error) {
|
|
data, err := staticFn(filename)
|
|
return template.CSS(data), err
|
|
},
|
|
}
|
|
)
|
|
|
|
func init() {
|
|
t := template.New("").Funcs(funcs)
|
|
|
|
var err error
|
|
Templates, err = t.ParseFS(templatesFS, "*")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
Static, err = fs.Sub(static, "static")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func staticFn(filename string) (string, error) {
|
|
f, err := Static.Open(filename)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer f.Close()
|
|
|
|
data, err := io.ReadAll(f)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
"filename": filename,
|
|
"data": string(data),
|
|
}).Debug("reading static file for template")
|
|
|
|
return string(data), nil
|
|
}
|