Some more things

This commit is contained in:
Simon Martens
2025-03-21 14:48:40 +01:00
parent 2550fd151e
commit 52382a00e2
5 changed files with 149 additions and 39 deletions

75
lenz.go
View File

@@ -1,16 +1,30 @@
package main
import (
"fmt"
"log/slog"
"path/filepath"
"time"
"github.com/Theodor-Springmann-Stiftung/lenz-web/config"
"github.com/Theodor-Springmann-Stiftung/lenz-web/git"
"github.com/Theodor-Springmann-Stiftung/lenz-web/xml"
gitprovider "github.com/Theodor-Springmann-Stiftung/lenz-web/git"
"github.com/Theodor-Springmann-Stiftung/lenz-web/server"
"github.com/Theodor-Springmann-Stiftung/lenz-web/templating"
"github.com/Theodor-Springmann-Stiftung/lenz-web/views"
xmlparsing "github.com/Theodor-Springmann-Stiftung/lenz-web/xml"
"github.com/Theodor-Springmann-Stiftung/lenz-web/xmlmodels"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/storage/memory/v2"
)
var REFRESH_CHANGES = []string{
"./views/assets",
}
var RESET_CHANGES = []string{
"./views/layouts",
"./views/routes",
}
func main() {
cfg, err := config.Get()
if err != nil {
@@ -29,8 +43,61 @@ func main() {
panic(err)
}
// INFO: the lib, engine and storage objects passed to the server are not made to
// be recreated.
lib := xmlmodels.NewLibrary()
lib.Parse(xmlparsing.Commit, dir, gp.Hash)
fmt.Println("Library: ", lib)
engine := templating.New(&views.LayoutFS, &views.RoutesFS)
storage := memory.New(memory.Config{
GCInterval: 24 * time.Hour,
})
if cfg.Debug {
SetupDebug(storage, engine)
}
server := server.New(engine, storage, cfg.Debug)
server.Start(cfg.Address + ":" + cfg.Port)
}
func SetupDebug(storage fiber.Storage, engine *templating.Engine) {
SetupRefreshWatcher(storage, engine)
SetupReloadWatcher(storage, engine)
engine.Debug()
}
func SetupRefreshWatcher(storage fiber.Storage, engine *templating.Engine) {
refreshwatcher, err := New(func() { RefreshFunction(storage, engine) })
if err != nil {
slog.Error("Error setting up refresh watcher, continuing without: ", "error:", err)
return
}
for _, path := range REFRESH_CHANGES {
refreshwatcher.AddRecursive(path)
}
}
func SetupReloadWatcher(storage fiber.Storage, engine *templating.Engine) {
resetwatcher, err := New(func() { ResetFunction(storage, engine) })
if err != nil {
slog.Error("Error setting up refresh watcher, continuing without: ", "error:", err)
return
}
for _, path := range RESET_CHANGES {
resetwatcher.AddRecursive(path)
}
}
func ResetFunction(storage fiber.Storage, engine *templating.Engine) {
storage.Reset()
engine.Reload()
}
func RefreshFunction(storage fiber.Storage, engine *templating.Engine) {
storage.Reset()
engine.Refresh()
}