frontend: simplify assets filesystem

This commit is contained in:
nkanaev
2026-06-25 16:05:19 +01:00
parent d900c85ffa
commit 4701f864ec
4 changed files with 27 additions and 59 deletions

View File

@@ -1,61 +1,19 @@
//go:build debug
package assets
import (
"embed"
"html/template"
"io"
"io/fs"
"log"
"os"
)
type assetsfs struct {
embedded *embed.FS
templates map[string]*template.Template
const rootDir = "src/assets"
func Templates() *template.Template {
return template.Must(template.ParseGlob(rootDir + "/templates/*.html"))
}
var FS assetsfs
func (afs assetsfs) Open(name string) (fs.File, error) {
if afs.embedded != nil {
return afs.embedded.Open(name)
}
return os.DirFS("src/assets").Open(name)
}
func Template(path string) *template.Template {
var tmpl *template.Template
tmpl, found := FS.templates[path]
if !found {
tmpl = template.Must(template.New(path).Delims("{%", "%}").Funcs(template.FuncMap{
"inline": func(svg string) template.HTML {
svgfile, err := FS.Open("graphicarts/" + svg)
// should never happen
if err != nil {
log.Fatal(err)
}
defer svgfile.Close()
content, err := io.ReadAll(svgfile)
// should never happen
if err != nil {
log.Fatal(err)
}
return template.HTML(content)
},
}).ParseFS(FS, path))
if FS.embedded != nil {
FS.templates[path] = tmpl
}
}
return tmpl
}
func Render(path string, writer io.Writer, data any) {
tmpl := Template(path)
tmpl.Execute(writer, data)
}
func init() {
FS.templates = make(map[string]*template.Template)
func StaticFS() fs.FS {
return os.DirFS(rootDir)
}

View File

@@ -2,14 +2,24 @@
package assets
import "embed"
import (
"embed"
"html/template"
"io/fs"
)
//go:embed *.html
//go:embed graphicarts
//go:embed javascripts
//go:embed stylesheets
var embedded embed.FS
var static embed.FS
func init() {
FS.embedded = &embedded
//go:embed templates
var templates embed.FS
func Templates() *template.Template {
return template.Must(template.ParseFS(templates, "*.html"))
}
func StaticFS() fs.FS {
return static
}

View File

@@ -44,7 +44,7 @@ func (m *Middleware) Handler(c *router.Context) {
c.Redirect(rootUrl)
return
} else {
c.HTML(http.StatusOK, assets.Template("login.html"), map[string]any{
c.HTML(http.StatusOK, assets.Templates().Lookup("login.html"), map[string]any{
"username": username,
"hasError": true,
"settings": m.DB.GetSettings().Map(),
@@ -52,7 +52,7 @@ func (m *Middleware) Handler(c *router.Context) {
return
}
}
c.HTML(http.StatusOK, assets.Template("login.html"), map[string]any{
c.HTML(http.StatusOK, assets.Templates().Lookup("login.html"), map[string]any{
"hasError": false,
"settings": m.DB.GetSettings().Map(),
})

View File

@@ -64,7 +64,7 @@ func (s *Server) handler() http.Handler {
}
func (s *Server) handleIndex(c *router.Context) {
c.HTML(http.StatusOK, assets.Template("index.html"), map[string]any{
c.HTML(http.StatusOK, assets.Templates().Lookup("index.html"), map[string]any{
"settings": s.db.GetSettings().Map(),
"authenticated": s.Username != "" && s.Password != "",
})
@@ -77,7 +77,7 @@ func (s *Server) handleStatic(c *router.Context) {
c.Out.WriteHeader(http.StatusNotFound)
return
}
http.StripPrefix(s.BasePath+"/static/", http.FileServer(http.FS(assets.FS))).
http.StripPrefix(s.BasePath+"/static/", http.FileServer(http.FS(assets.StaticFS()))).
ServeHTTP(c.Out, c.Req)
}