mirror of
https://github.com/nkanaev/yarr.git
synced 2026-07-15 19:16:32 +00:00
frontend: simplify assets filesystem
This commit is contained in:
@@ -1,61 +1,19 @@
|
|||||||
|
//go:build debug
|
||||||
|
|
||||||
package assets
|
package assets
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
type assetsfs struct {
|
const rootDir = "src/assets"
|
||||||
embedded *embed.FS
|
|
||||||
templates map[string]*template.Template
|
func Templates() *template.Template {
|
||||||
|
return template.Must(template.ParseGlob(rootDir + "/templates/*.html"))
|
||||||
}
|
}
|
||||||
|
|
||||||
var FS assetsfs
|
func StaticFS() fs.FS {
|
||||||
|
return os.DirFS(rootDir)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,14 +2,24 @@
|
|||||||
|
|
||||||
package assets
|
package assets
|
||||||
|
|
||||||
import "embed"
|
import (
|
||||||
|
"embed"
|
||||||
|
"html/template"
|
||||||
|
"io/fs"
|
||||||
|
)
|
||||||
|
|
||||||
//go:embed *.html
|
|
||||||
//go:embed graphicarts
|
//go:embed graphicarts
|
||||||
//go:embed javascripts
|
//go:embed javascripts
|
||||||
//go:embed stylesheets
|
//go:embed stylesheets
|
||||||
var embedded embed.FS
|
var static embed.FS
|
||||||
|
|
||||||
func init() {
|
//go:embed templates
|
||||||
FS.embedded = &embedded
|
var templates embed.FS
|
||||||
|
|
||||||
|
func Templates() *template.Template {
|
||||||
|
return template.Must(template.ParseFS(templates, "*.html"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func StaticFS() fs.FS {
|
||||||
|
return static
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ func (m *Middleware) Handler(c *router.Context) {
|
|||||||
c.Redirect(rootUrl)
|
c.Redirect(rootUrl)
|
||||||
return
|
return
|
||||||
} else {
|
} 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,
|
"username": username,
|
||||||
"hasError": true,
|
"hasError": true,
|
||||||
"settings": m.DB.GetSettings().Map(),
|
"settings": m.DB.GetSettings().Map(),
|
||||||
@@ -52,7 +52,7 @@ func (m *Middleware) Handler(c *router.Context) {
|
|||||||
return
|
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,
|
"hasError": false,
|
||||||
"settings": m.DB.GetSettings().Map(),
|
"settings": m.DB.GetSettings().Map(),
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ func (s *Server) handler() http.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) handleIndex(c *router.Context) {
|
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(),
|
"settings": s.db.GetSettings().Map(),
|
||||||
"authenticated": s.Username != "" && s.Password != "",
|
"authenticated": s.Username != "" && s.Password != "",
|
||||||
})
|
})
|
||||||
@@ -77,7 +77,7 @@ func (s *Server) handleStatic(c *router.Context) {
|
|||||||
c.Out.WriteHeader(http.StatusNotFound)
|
c.Out.WriteHeader(http.StatusNotFound)
|
||||||
return
|
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)
|
ServeHTTP(c.Out, c.Req)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user