mirror of
https://github.com/Theodor-Springmann-Stiftung/kgpz_web.git
synced 2025-12-15 19:45:30 +00:00
bugfix
This commit is contained in:
@@ -4,72 +4,27 @@ import (
|
|||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"path/filepath"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/Theodor-Springmann-Stiftung/kgpz_web/helpers"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Engine struct {
|
type Engine struct {
|
||||||
// NOTE: LayoutRegistry and TemplateRegistry have their own syncronization & cache and do not require a mutex here
|
// NOTE: LayoutRegistry and TemplateRegistry have their own syncronization & cache and do not require a mutex here
|
||||||
regmu *sync.Mutex
|
|
||||||
LayoutRegistry *LayoutRegistry
|
LayoutRegistry *LayoutRegistry
|
||||||
TemplateRegistry *TemplateRegistry
|
TemplateRegistry *TemplateRegistry
|
||||||
|
|
||||||
mu *sync.Mutex
|
mu *sync.Mutex
|
||||||
FuncMap template.FuncMap
|
FuncMap template.FuncMap
|
||||||
|
|
||||||
paths []string
|
|
||||||
layouts *fs.FS
|
|
||||||
templates *fs.FS
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEngine(layouts, templates *fs.FS) *Engine {
|
func NewEngine(layouts, templates *fs.FS) *Engine {
|
||||||
return &Engine{
|
return &Engine{
|
||||||
regmu: &sync.Mutex{},
|
|
||||||
mu: &sync.Mutex{},
|
mu: &sync.Mutex{},
|
||||||
LayoutRegistry: NewLayoutRegistry(*layouts),
|
LayoutRegistry: NewLayoutRegistry(*layouts),
|
||||||
TemplateRegistry: NewTemplateRegistry(*templates),
|
TemplateRegistry: NewTemplateRegistry(*templates),
|
||||||
FuncMap: template.FuncMap{},
|
FuncMap: template.FuncMap{},
|
||||||
layouts: layouts,
|
|
||||||
templates: templates,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Engine) AddWatchers(paths []string) error {
|
|
||||||
e.paths = paths
|
|
||||||
var dirs []string
|
|
||||||
for _, path := range paths {
|
|
||||||
// Get all subdirectories for paths
|
|
||||||
filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error {
|
|
||||||
if d.IsDir() {
|
|
||||||
dirs = append(dirs, path)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
watcher, err := helpers.NewFileWatcher(dirs)
|
|
||||||
defer watcher.Close()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
w := watcher.GetEvents()
|
|
||||||
<-w
|
|
||||||
time.Sleep(100 * time.Millisecond)
|
|
||||||
e.regmu.Lock()
|
|
||||||
defer e.regmu.Unlock()
|
|
||||||
e.LayoutRegistry = NewLayoutRegistry(*e.layouts)
|
|
||||||
e.TemplateRegistry = NewTemplateRegistry(*e.templates)
|
|
||||||
e.AddWatchers(e.paths)
|
|
||||||
}()
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Engine) Load() error {
|
func (e *Engine) Load() error {
|
||||||
wg := sync.WaitGroup{}
|
wg := sync.WaitGroup{}
|
||||||
wg.Add(2)
|
wg.Add(2)
|
||||||
|
|||||||
Reference in New Issue
Block a user