This commit is contained in:
Simon Martens
2025-03-05 16:41:39 +01:00
commit e19fd47c17
88 changed files with 9765 additions and 0 deletions

14
templating/consts.go Normal file
View File

@@ -0,0 +1,14 @@
package templating
var TEMPLATE_FORMATS = []string{".html", ".tmpl", ".gotmpl", ".gotemplate", ".gohtml", ".gohtmltemplate"}
const TEMPLATE_GLOBAL_CONTEXT_NAME = "globals"
const TEMPLATE_ROOT_NAME = "root"
const TEMPLATE_LOCAL_CONTEXT_NAME = "locals"
const TEMPLATE_GLOBAL_PREFIX = "_"
const TEMPLATE_COMPONENT_DIRECTORY = "components"
const TEMPLATE_HEAD = "head"
const TEMPLATE_BODY = "body"
const TEMPLATE_HEADERS = "headers"
const ROOT_LAYOUT_NAME = "root"
const DEFAULT_LAYOUT_NAME = "default"

148
templating/context.go Normal file
View File

@@ -0,0 +1,148 @@
package templating
import (
"html/template"
"io/fs"
"path/filepath"
"slices"
"strings"
)
type TemplateContext struct {
// WARNING: Path is a URL path, NOT a filesystem path
Path string
// WARNING: The keys of these maps are template names, NOT filesystem paths
// The values are FS paths absolute from the root directory of the templates FS
locals map[string]string
globals map[string]string
}
func NewTemplateContext(path string) TemplateContext {
return TemplateContext{
Path: path,
locals: make(map[string]string),
globals: make(map[string]string),
}
}
func (c *TemplateContext) Parse(fsys fs.FS) error {
fspath := PathToFSPath(c.Path)
entries, err := fs.ReadDir(fsys, fspath)
if err != nil {
return NewError(InvalidPathError, c.Path)
}
for _, e := range entries {
if e.IsDir() {
// INFO: components in the components directory can be overwritten
// by components in the base directory down below
// TODO: Maybe allow for subdirectories in the components directory?
if e.Name() == TEMPLATE_COMPONENT_DIRECTORY {
entries, err := fs.ReadDir(fsys, filepath.Join(fspath, e.Name()))
if err != nil {
return NewError(FileAccessError, filepath.Join(fspath, e.Name()))
}
for _, e := range entries {
ext := filepath.Ext(e.Name())
if !slices.Contains(TEMPLATE_FORMATS, ext) {
continue
}
name := strings.TrimSuffix(e.Name(), ext)
if strings.HasPrefix(e.Name(), TEMPLATE_GLOBAL_PREFIX) {
c.globals[name] = filepath.Join(fspath, TEMPLATE_COMPONENT_DIRECTORY, e.Name())
} else {
c.locals[name] = filepath.Join(fspath, TEMPLATE_COMPONENT_DIRECTORY, e.Name())
}
}
continue
}
}
ext := filepath.Ext(e.Name())
if !slices.Contains(TEMPLATE_FORMATS, ext) {
continue
}
name := strings.TrimSuffix(e.Name(), ext)
if strings.HasPrefix(e.Name(), TEMPLATE_GLOBAL_PREFIX) {
c.globals[name] = filepath.Join(fspath, e.Name())
} else {
c.locals[name] = filepath.Join(fspath, e.Name())
}
}
return nil
}
func (c *TemplateContext) SetGlobals(globals map[string]string) error {
// INFO: this allows for overwriting of existing global keys.
// Make sure to call this appopriately before or after Parse(), depending on your use case
for k, v := range globals {
c.globals[k] = v
}
return nil
}
func (c *TemplateContext) Globals() map[string]string {
return c.globals
}
func (c *TemplateContext) Template(fsys fs.FS, funcmap *template.FuncMap) (*template.Template, error) {
// TODO: locals need to be in order: root, head, body
t, err := readTemplates(fsys, nil, c.locals, funcmap)
if err != nil {
return nil, err
}
t, err = readTemplates(fsys, t, c.globals, funcmap)
if err != nil {
return nil, err
}
return t, nil
}
func readTemplates(fsys fs.FS, t *template.Template, paths map[string]string, funcmap *template.FuncMap) (*template.Template, error) {
for k, v := range paths {
text, err := fs.ReadFile(fsys, v)
if err != nil {
return nil, NewError(FileAccessError, v)
}
temp := template.New(k)
if funcmap != nil {
temp.Funcs(*funcmap)
}
temp, err = temp.Parse(string(text))
if err != nil {
return nil, err
}
if t == nil {
t = temp
continue
}
for _, template := range temp.Templates() {
_, err = t.AddParseTree(template.Name(), template.Tree)
if err != nil {
return nil, err
}
}
_, err = t.AddParseTree(temp.Name(), temp.Tree)
if err != nil {
return nil, err
}
}
return t, nil
}

249
templating/engine.go Normal file
View File

@@ -0,0 +1,249 @@
package templating
import (
"html/template"
"io"
"io/fs"
"log"
"net/http"
"sync"
"github.com/Theodor-Springmann-Stiftung/lenz-web/helpers/functions"
"golang.org/x/net/websocket"
)
const (
ASSETS_URL_PREFIX = "/assets"
RELOAD_TEMPLATE = `
<script type="module">
(function () {
let relto = -1;
const scheme = location.protocol === "https:" ? "wss" : "ws";
// Hardcode port 9000 here:
const url = scheme + "://" + location.hostname + ":9000/pb/reload";
function connect() {
const socket = new WebSocket(url);
socket.addEventListener("open", function () {
console.log("Reload socket connected (port 9000).");
});
socket.addEventListener("message", function (evt) {
if (evt.data === "reload") {
console.log("Received reload signal. Reloading...");
if (relto !== -1) clearTimeout(relto);
relto = setTimeout(() => location.reload(), 0);
}
});
socket.addEventListener("close", function () {
console.log("Reload socket closed. Reconnecting in 3 seconds...");
setTimeout(connect, 3000);
});
socket.addEventListener("error", function (err) {
console.error("Reload socket error:", err);
// We'll let onclose handle reconnection.
});
}
// Initiate the first connection attempt.
connect();
})();
</script>
`
)
type Engine struct {
regmu *sync.Mutex
debug bool
ws *WsServer
onceWS sync.Once
// NOTE: LayoutRegistry and TemplateRegistry have their own syncronization & cache and do not require a mutex here
LayoutRegistry *LayoutRegistry
TemplateRegistry *TemplateRegistry
mu *sync.Mutex
FuncMap template.FuncMap
GlobalData map[string]interface{}
}
// INFO: We pass the app here to be able to access the config and other data for functions
// which also means we must reload the engine if the app changes
func NewEngine(layouts, templates *fs.FS) *Engine {
e := Engine{
regmu: &sync.Mutex{},
mu: &sync.Mutex{},
LayoutRegistry: NewLayoutRegistry(*layouts),
TemplateRegistry: NewTemplateRegistry(*templates),
FuncMap: make(template.FuncMap),
GlobalData: make(map[string]interface{}),
}
e.funcs()
return &e
}
func (e *Engine) Debug() {
e.debug = true
e.onceWS.Do(func() {
e.ws = NewWsServer()
go e.startWsServerOnPort9000()
})
}
func (e *Engine) startWsServerOnPort9000() {
// We'll create a basic default mux here and mount /pb/reload
mux := http.NewServeMux()
mux.Handle("/pb/reload", websocket.Handler(e.ws.Handler))
log.Println("[Engine Debug] Starting separate WebSocket server on :9000 for live reload...")
if err := http.ListenAndServe(":9000", mux); err != nil {
log.Println("[Engine Debug] WebSocket server error:", err)
}
}
func (e *Engine) funcs() error {
e.mu.Lock()
e.mu.Unlock()
// Passing HTML
e.AddFunc("Safe", functions.Safe)
// Creating an array or dict (to pass to a template)
// e.AddFunc("Arr", functions.Arr)
// e.AddFunc("Dict", functions.Dict)
// Datatype Functions
// e.AddFunc("HasPrefix", strings.HasPrefix)
// e.AddFunc("Contains", functions.Contains)
// e.AddFunc("Add", functions.Add)
// e.AddFunc("Len", functions.Length)
// String Functions
// e.AddFunc("Lower", functions.Lower)
// e.AddFunc("Upper", functions.Upper)
// e.AddFunc("First", functions.First)
// e.AddFunc("ReplaceSlashParen", functions.ReplaceSlashParen)
// e.AddFunc("ReplaceSlashParenSlash", functions.ReplaceSlashParenSlash)
// e.AddFunc("LinksAnnotation", functions.LinksAnnotation)
//
// Time & Date Functions
// e.AddFunc("Today", functions.Today)
// e.AddFunc("GetMonth", functions.GetMonth)
//
// // TOC
// e.AddFunc("TOCFromHTML", functions.TOCFromHTML)
return nil
}
func (e *Engine) Globals(data map[string]interface{}) {
e.mu.Lock()
defer e.mu.Unlock()
if e.GlobalData == nil {
e.GlobalData = data
} else {
for k, v := range data {
(e.GlobalData)[k] = v
}
}
}
func (e *Engine) Load() {
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
defer wg.Done()
e.LayoutRegistry.Load()
}()
go func() {
defer wg.Done()
e.TemplateRegistry.Load()
}()
wg.Wait()
}
func (e *Engine) Reload() {
e.regmu.Lock()
defer e.regmu.Unlock()
e.LayoutRegistry = e.LayoutRegistry.Reset()
e.TemplateRegistry = e.TemplateRegistry.Reset()
e.Load()
}
func (e *Engine) Refresh() {
if e.debug && e.ws != nil {
e.ws.BroadcastReload()
}
}
// INFO: fn is a function that returns either one value or two values, the second one being an error
func (e *Engine) AddFunc(name string, fn interface{}) {
e.mu.Lock()
defer e.mu.Unlock()
e.FuncMap[name] = fn
}
func (e *Engine) AddFuncs(funcs map[string]interface{}) {
e.mu.Lock()
defer e.mu.Unlock()
for k, v := range funcs {
e.FuncMap[k] = v
}
}
func (e *Engine) Render(out io.Writer, path string, ld map[string]interface{}, layout ...string) error {
gd := e.GlobalData
if ld == nil {
ld = make(map[string]interface{})
}
// INFO: don't pollute the global data space
for k, v := range gd {
_, ok := ld[k]
if !ok {
ld[k] = v
}
}
e.mu.Lock()
defer e.mu.Unlock()
e.regmu.Lock()
defer e.regmu.Unlock()
var l *template.Template
if layout == nil || len(layout) == 0 {
lay, err := e.LayoutRegistry.Default(&e.FuncMap)
if err != nil {
return err
}
l = lay
} else {
lay, err := e.LayoutRegistry.Layout(layout[0], &e.FuncMap)
if err != nil {
return err
}
l = lay
}
lay, err := l.Clone()
if err != nil {
return err
}
err = e.TemplateRegistry.Add(path, lay, &e.FuncMap)
if err != nil {
return err
}
err = lay.Execute(out, ld)
if err != nil {
return err
}
return nil
}

21
templating/errors.go Normal file
View File

@@ -0,0 +1,21 @@
package templating
import "errors"
var InvalidPathError = errors.New("Invalid path. Must be a directory.")
var NoTemplateError = errors.New("No template found for this name")
var InvalidTemplateError = errors.New("invalid template")
var FileAccessError = errors.New("could not stat file or directory")
type FSError[T error] struct {
File string
Err T
}
func NewError[T error](t T, file string) FSError[T] {
return FSError[T]{File: file, Err: t}
}
func (e FSError[T]) Error() string {
return e.Err.Error() + ": " + e.File
}

32
templating/helpers.go Normal file
View File

@@ -0,0 +1,32 @@
package templating
import "strings"
func PathToFSPath(p string) string {
if p == "/" {
return "."
}
p = strings.TrimPrefix(p, "/")
p = strings.TrimSuffix(p, "/")
return p
}
func FSPathToPath(p string) string {
if p == "." {
return "/"
}
p = strings.TrimPrefix(p, ".")
if !strings.HasPrefix(p, "/") {
p = "/" + p
}
if !strings.HasSuffix(p, "/") {
p = p + "/"
}
return p
}

View File

@@ -0,0 +1,108 @@
package templating
import (
"html/template"
"io/fs"
"sync"
"github.com/yalue/merged_fs"
)
// TODO: Implement Handler interface, maybe in template? But then template would need to know about the layout registry
// Function signature: func (t *templateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
// A static Handler could incoporate both the layout registry and the template registry and serve templates that dont need any data
// INFO: this ist thread-safe and safe to call in a handler or middleware
type LayoutRegistry struct {
layoutsFS fs.FS
once sync.Once
// INFO: Layout & cache keys are template directory names
layouts map[string]TemplateContext
// WARNING: maybe this is too early for caching?
cache sync.Map
}
func NewLayoutRegistry(routes fs.FS) *LayoutRegistry {
return &LayoutRegistry{
layoutsFS: routes,
}
}
// NOTE: Upon registering a new layout dir, we return a new LayoutRegistry
func (r *LayoutRegistry) Register(fs fs.FS) *LayoutRegistry {
return NewLayoutRegistry(merged_fs.MergeMultiple(fs, r.layoutsFS))
}
func (r *LayoutRegistry) Reset() *LayoutRegistry {
return NewLayoutRegistry(r.layoutsFS)
}
func (r *LayoutRegistry) Load() error {
var outer error
r.once.Do(func() {
err := r.load()
if err != nil {
outer = err
}
})
return outer
}
func (r *LayoutRegistry) load() error {
layouts := make(map[string]TemplateContext)
rootcontext := NewTemplateContext(".")
err := rootcontext.Parse(r.layoutsFS)
if err != nil {
return err
}
globals := rootcontext.Globals()
entries, err := fs.ReadDir(r.layoutsFS, ".")
if err != nil {
return NewError(FileAccessError, ".")
}
for _, e := range entries {
if !e.IsDir() || e.Name() == TEMPLATE_COMPONENT_DIRECTORY {
continue
}
url := FSPathToPath(e.Name())
context := NewTemplateContext(url)
context.SetGlobals(globals)
context.Parse(r.layoutsFS)
layouts[e.Name()] = context
}
r.layouts = layouts
return nil
}
func (r *LayoutRegistry) Layout(name string, funcmap *template.FuncMap) (*template.Template, error) {
cached, ok := r.cache.Load(name)
if ok {
return cached.(*template.Template), nil
}
// TODO: What todo on errors?
r.Load()
context, ok := r.layouts[name]
if !ok {
return nil, NewError(NoTemplateError, name)
}
t, err := context.Template(r.layoutsFS, funcmap)
if err != nil {
return nil, err
}
r.cache.Store(name, t)
return t, nil
}
func (r *LayoutRegistry) Default(funcmap *template.FuncMap) (*template.Template, error) {
return r.Layout(DEFAULT_LAYOUT_NAME, funcmap)
}

View File

@@ -0,0 +1,121 @@
package templating
import (
"html/template"
"io/fs"
"os"
"strings"
"sync"
"github.com/yalue/merged_fs"
)
// INFO: this ist thread-safe and safe to call in a handler or middleware
type TemplateRegistry struct {
routesFS fs.FS
once sync.Once
// INFO: Template & cache keys are directory routing paths, with '/' as root
// INFO: we don't need a mutex here since this is set in Load() protected by Once().
templates map[string]TemplateContext
cache sync.Map
}
func NewTemplateRegistry(routes fs.FS) *TemplateRegistry {
return &TemplateRegistry{
routesFS: routes,
}
}
// INFO: This returns a new TemplateRegistry with the new fs added to the existing fs,
// merging with the existing FS, possibly overwriting existing files.
func (r *TemplateRegistry) Register(path string, fs fs.FS) *TemplateRegistry {
return NewTemplateRegistry(merged_fs.MergeMultiple(fs, r.routesFS))
}
func (r *TemplateRegistry) Reset() *TemplateRegistry {
return NewTemplateRegistry(r.routesFS)
}
func (r *TemplateRegistry) Load() error {
var outer error
r.once.Do(func() {
err := r.load()
if err != nil {
outer = err
}
})
return outer
}
// TODO: Throw errors
// TODO: what if there is no template in the directory above?
// What if a certain path is or should uncallable since it has no index or body?
func (r *TemplateRegistry) load() error {
templates := make(map[string]TemplateContext)
fs.WalkDir(r.routesFS, ".", func(path string, d fs.DirEntry, err error) error {
if !d.IsDir() {
return nil
}
url := FSPathToPath(path)
tc := NewTemplateContext(url)
if path != "." {
pathelem := strings.Split(path, string(os.PathSeparator))
pathabove := strings.Join(pathelem[:len(pathelem)-1], string(os.PathSeparator))
pathabove = FSPathToPath(pathabove)
global, ok := templates[pathabove]
if ok {
tc.SetGlobals(global.Globals())
}
}
tc.Parse(r.routesFS)
templates[url] = tc
return nil
})
r.templates = templates
return nil
}
// This function takes a template (typically a layout) and adds all the templates of
// a given directory path to it. This is useful for adding a layout to a template.
func (r *TemplateRegistry) Add(path string, t *template.Template, funcmap *template.FuncMap) error {
temp, ok := r.cache.Load(path)
if !ok {
r.Load()
tc, ok := r.templates[path]
if !ok {
return NewError(NoTemplateError, path)
}
template, err := tc.Template(r.routesFS, funcmap)
if err != nil {
return err
}
r.cache.Store(path, template)
return r.Add(path, t, funcmap)
}
casted := temp.(*template.Template)
for _, st := range casted.Templates() {
_, err := t.AddParseTree(st.Name(), st.Tree)
if err != nil {
return err
}
}
return nil
}
// TODO: get for a specific component
func (r *TemplateRegistry) Get(path string) error {
return nil
}

57
templating/ws.go Normal file
View File

@@ -0,0 +1,57 @@
package templating
import (
"log"
"sync"
"golang.org/x/net/websocket"
)
// WsServer manages all active WebSocket connections so we can broadcast.
type WsServer struct {
mu sync.Mutex
conns map[*websocket.Conn]bool
}
// NewWsServer creates a WsServer.
func NewWsServer() *WsServer {
return &WsServer{
conns: make(map[*websocket.Conn]bool),
}
}
// Handler is invoked for each new WebSocket connection.
func (s *WsServer) Handler(conn *websocket.Conn) {
s.mu.Lock()
s.conns[conn] = true
s.mu.Unlock()
log.Println("[WsServer] Connected:", conn.RemoteAddr())
// Read in a loop until an error (client disconnect).
var msg string
for {
if err := websocket.Message.Receive(conn, &msg); err != nil {
log.Println("[WsServer] Disconnected:", conn.RemoteAddr())
s.mu.Lock()
delete(s.conns, conn)
s.mu.Unlock()
conn.Close()
return
}
}
}
// BroadcastReload sends a "reload" message to all connected clients.
func (s *WsServer) BroadcastReload() {
s.mu.Lock()
defer s.mu.Unlock()
for conn := range s.conns {
err := websocket.Message.Send(conn, "reload")
if err != nil {
log.Println("[WsServer] Broadcast error:", err)
conn.Close()
delete(s.conns, conn)
}
}
}