Restart Init

This commit is contained in:
Simon Martens
2026-02-18 13:41:44 +01:00
parent 938cdeb27b
commit 4f4288905d
2955 changed files with 4795 additions and 53375 deletions

92
lenz.go
View File

@@ -1,33 +1,15 @@
package main
import (
"fmt"
"log/slog"
"path/filepath"
"time"
"github.com/Theodor-Springmann-Stiftung/lenz-web/config"
"github.com/Theodor-Springmann-Stiftung/lenz-web/controllers"
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"
"github.com/Theodor-Springmann-Stiftung/lenz-web/xmlmodels"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/storage/memory/v2"
"github.com/Theodor-Springmann-Stiftung/lenz-web/app"
"github.com/Theodor-Springmann-Stiftung/lenz-web/git"
)
var REFRESH_CHANGES = []string{
"./views/assets",
}
var RESET_CHANGES = []string{
"./views/layouts",
"./views/routes",
}
func main() {
cfg, err := config.Get()
cfg, err := GetConfig()
if err != nil {
panic(err)
}
@@ -37,74 +19,22 @@ func main() {
}
dir := filepath.Join(cfg.BaseDIR, cfg.GITPath)
fmt.Printf("Starting Lenz with config: %v", cfg)
commit, err := gitprovider.OpenOrClone(dir, cfg.GitURL, cfg.GitBranch)
repo, err := git.OpenOrClone(dir, cfg.GitURL, cfg.GitBranch)
if err != nil {
panic(err)
}
// INFO: the lib, engine and storage objects passed to the server should never be recreated.
lib, err := xmlmodels.Parse(dir, commit.Hash)
println("Repo opened: " + repo.String())
latest, err := repo.Latest()
if err != nil {
panic(err)
}
engine := templating.New(&views.LayoutFS, &views.RoutesFS)
engine.AddFuncs(lib.FuncMap())
engine.AddFunc("ParseGeneric", xmlmodels.TemplateParse(lib))
storage := memory.New(memory.Config{
GCInterval: 24 * time.Hour,
})
if cfg.Debug {
SetupDebug(storage, engine)
}
server := server.New(engine, storage, cfg.Debug)
controllers.Register(server, cfg)
server.Start(cfg.Address + ":" + cfg.Port)
println("Commit" + latest.String())
}
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) {
slog.Debug("Resetting storage and reloading engine")
storage.Reset()
engine.Reload()
}
func RefreshFunction(storage fiber.Storage, engine *templating.Engine) {
slog.Debug("Resetting storage and sending refresh signal")
storage.Reset()
engine.Refresh()
func GetConfig() (app.Config, error) {
cp := app.NewConfigProvider([]string{"config.dev.json", "config.json"})
return *cp.Config, cp.Read()
}