mirror of
https://github.com/Theodor-Springmann-Stiftung/musenalm.git
synced 2026-02-04 02:25:30 +00:00
BUG: acces to app struct ifrom the controllers is missing
This commit is contained in:
62
app/pb.go
62
app/pb.go
@@ -3,6 +3,7 @@ package app
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/Theodor-Springmann-Stiftung/musenalm/dbmodels"
|
"github.com/Theodor-Springmann-Stiftung/musenalm/dbmodels"
|
||||||
"github.com/Theodor-Springmann-Stiftung/musenalm/middleware"
|
"github.com/Theodor-Springmann-Stiftung/musenalm/middleware"
|
||||||
@@ -26,9 +27,13 @@ type BootFunc = func(e *core.BootstrapEvent) error
|
|||||||
|
|
||||||
// INFO: this is the main application that mainly is a pocketbase wrapper
|
// INFO: this is the main application that mainly is a pocketbase wrapper
|
||||||
type App struct {
|
type App struct {
|
||||||
PB *pocketbase.PocketBase
|
PB *pocketbase.PocketBase
|
||||||
MAConfig Config
|
MAConfig Config
|
||||||
Pages []pagemodels.IPage
|
Pages []pagemodels.IPage
|
||||||
|
dataCache map[string]any
|
||||||
|
dataMutex sync.RWMutex
|
||||||
|
htmlCache map[string]any
|
||||||
|
htmlMutex sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -148,17 +153,60 @@ func (app *App) createEngine() (*templating.Engine, error) {
|
|||||||
"desc": "Bibliographie deutscher Almanache des 18. und 19. Jahrhunderts",
|
"desc": "Bibliographie deutscher Almanache des 18. und 19. Jahrhunderts",
|
||||||
}})
|
}})
|
||||||
|
|
||||||
|
app.ResetDataCache()
|
||||||
engine.AddFunc("data", func(key string) any {
|
engine.AddFunc("data", func(key string) any {
|
||||||
res, err := dbmodels.Data_Key(app.PB.App, key)
|
if len(app.dataCache) == 0 {
|
||||||
if err != nil {
|
data, err := dbmodels.Data_All(app.PB.App)
|
||||||
return "{}"
|
if err != nil {
|
||||||
|
app.PB.Logger().Error("Failed to fetch data cache: %v", err)
|
||||||
|
return "{}"
|
||||||
|
}
|
||||||
|
app.dataMutex.Lock()
|
||||||
|
for _, d := range data {
|
||||||
|
app.dataCache[d.Key()] = d.Value()
|
||||||
|
}
|
||||||
|
app.dataMutex.Unlock()
|
||||||
}
|
}
|
||||||
return res.Value()
|
app.dataMutex.RLock()
|
||||||
|
defer app.dataMutex.RUnlock()
|
||||||
|
return app.dataCache[key]
|
||||||
|
})
|
||||||
|
|
||||||
|
app.ResetHtmlCache()
|
||||||
|
engine.AddFunc("html", func(key string) any {
|
||||||
|
if len(app.htmlCache) == 0 {
|
||||||
|
html, err := dbmodels.Html_All(app.PB.App)
|
||||||
|
if err != nil {
|
||||||
|
app.PB.Logger().Error("Failed to fetch html cache: %v", err)
|
||||||
|
return "{}"
|
||||||
|
}
|
||||||
|
app.htmlMutex.Lock()
|
||||||
|
for _, h := range html {
|
||||||
|
app.htmlCache[h.Key()] = h.HTML()
|
||||||
|
}
|
||||||
|
app.htmlMutex.Unlock()
|
||||||
|
}
|
||||||
|
app.htmlMutex.RLock()
|
||||||
|
defer app.htmlMutex.RUnlock()
|
||||||
|
return app.htmlCache[key]
|
||||||
})
|
})
|
||||||
|
|
||||||
return engine, nil
|
return engine, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BUG: we cant call this from the templates, bc this App struct is not available
|
||||||
|
func (app *App) ResetDataCache() {
|
||||||
|
app.dataMutex.Lock()
|
||||||
|
defer app.dataMutex.Unlock()
|
||||||
|
app.dataCache = make(map[string]any)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *App) ResetHtmlCache() {
|
||||||
|
app.htmlMutex.Lock()
|
||||||
|
defer app.htmlMutex.Unlock()
|
||||||
|
app.htmlCache = make(map[string]any)
|
||||||
|
}
|
||||||
|
|
||||||
func (app *App) setWatchers(engine *templating.Engine) {
|
func (app *App) setWatchers(engine *templating.Engine) {
|
||||||
// INFO: hot reloading for poor people
|
// INFO: hot reloading for poor people
|
||||||
watcher, err := EngineWatcher(engine)
|
watcher, err := EngineWatcher(engine)
|
||||||
|
|||||||
@@ -14,8 +14,8 @@ func (h *HTML) SetKey(key string) {
|
|||||||
h.Set(KEY_FIELD, key)
|
h.Set(KEY_FIELD, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *HTML) HTML() string {
|
func (h *HTML) HTML() any {
|
||||||
return h.GetString(HTML_FIELD)
|
return h.GetRaw(HTML_FIELD)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *HTML) SetHTML(html string) {
|
func (h *HTML) SetHTML(html string) {
|
||||||
|
|||||||
@@ -149,6 +149,18 @@ func Data_Key(app core.App, key string) (*Data, error) {
|
|||||||
return &ret, err
|
return &ret, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Data_All(app core.App) ([]*Data, error) {
|
||||||
|
data := make([]*Data, 0)
|
||||||
|
err := app.RecordQuery(DATA_TABLE).All(&data)
|
||||||
|
return data, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func Html_All(app core.App) ([]*HTML, error) {
|
||||||
|
html := make([]*HTML, 0)
|
||||||
|
err := app.RecordQuery(HTML_TABLE).All(&html)
|
||||||
|
return html, err
|
||||||
|
}
|
||||||
|
|
||||||
func AccessTokens_Token(app core.App, token string) (*AccessToken, error) {
|
func AccessTokens_Token(app core.App, token string) (*AccessToken, error) {
|
||||||
t := HashStringSHA256(token)
|
t := HashStringSHA256(token)
|
||||||
return TableByField[*AccessToken](
|
return TableByField[*AccessToken](
|
||||||
|
|||||||
Reference in New Issue
Block a user