2024-04-24 00:34:57 +00:00
|
|
|
package frontend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"html/template"
|
2024-04-25 19:06:20 +00:00
|
|
|
"io"
|
2024-04-24 00:34:57 +00:00
|
|
|
"io/fs"
|
2024-04-25 19:06:20 +00:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2024-04-24 00:34:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
//go:embed static
|
|
|
|
static embed.FS
|
|
|
|
Static fs.FS
|
|
|
|
|
|
|
|
//go:embed *.html
|
|
|
|
templatesFS embed.FS
|
|
|
|
Templates *template.Template
|
2024-04-25 19:06:20 +00:00
|
|
|
|
|
|
|
funcs = template.FuncMap{
|
2024-04-25 19:38:24 +00:00
|
|
|
"version": func() string {
|
|
|
|
return "better-zwave-locks v0.x.x aaaaaaaa"
|
2024-04-25 19:06:20 +00:00
|
|
|
},
|
|
|
|
}
|
2024-04-24 00:34:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2024-04-25 19:06:20 +00:00
|
|
|
t := template.New("").Funcs(funcs)
|
|
|
|
|
2024-04-24 00:34:57 +00:00
|
|
|
var err error
|
2024-04-25 19:06:20 +00:00
|
|
|
Templates, err = t.ParseFS(templatesFS, "*")
|
2024-04-24 00:34:57 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
Static, err = fs.Sub(static, "static")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2024-04-25 19:06:20 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|