lavender/server/pages/pages.go

46 lines
843 B
Go
Raw Normal View History

2023-10-08 15:24:59 +01:00
package pages
import (
"embed"
_ "embed"
"github.com/1f349/overlapfs"
2023-10-08 15:24:59 +01:00
"html/template"
2023-10-09 00:04:28 +01:00
"io"
"io/fs"
"log"
2023-10-08 15:24:59 +01:00
"os"
"path/filepath"
2023-10-09 00:04:28 +01:00
"sync"
2023-10-08 15:24:59 +01:00
)
var (
//go:embed *.go.html
2023-10-08 15:24:59 +01:00
flowPages embed.FS
2023-10-09 00:04:28 +01:00
flowTemplates *template.Template
loadOnce sync.Once
2023-10-08 15:24:59 +01:00
)
2023-10-09 00:04:28 +01:00
func LoadPages(wd string) (err error) {
loadOnce.Do(func() {
var o fs.FS = flowPages
if wd != "" {
wwwDir := filepath.Join(wd, "www")
err = os.Mkdir(wwwDir, os.ModePerm)
if err != nil {
return
}
wdFs := os.DirFS(wwwDir)
o = overlapfs.OverlapFS{A: flowPages, B: wdFs}
}
flowTemplates, err = template.ParseFS(o, "*.go.html")
})
return err
}
func RenderPageTemplate(wr io.Writer, name string, data any) {
err := flowTemplates.ExecuteTemplate(wr, name+".go.html", data)
if err != nil {
2023-10-09 00:04:28 +01:00
log.Printf("Failed to render page: %s: %s\n", name, err)
}
2023-10-08 15:24:59 +01:00
}