diff --git a/controllers/routes.go b/controllers/routes.go new file mode 100644 index 0000000..e487caf --- /dev/null +++ b/controllers/routes.go @@ -0,0 +1,13 @@ +package controllers + +import ( + "github.com/Theodor-Springmann-Stiftung/lenz-web/helpers/middleware" + "github.com/Theodor-Springmann-Stiftung/lenz-web/server" + "github.com/Theodor-Springmann-Stiftung/lenz-web/views" +) + +const ASSETS_URL = "/assets" + +func Register(server server.Server) { + server.Server.Use(ASSETS_URL, middleware.StaticHandler(&views.StaticFS)) +} diff --git a/helpers/middleware/static.go b/helpers/middleware/static.go new file mode 100644 index 0000000..040be9e --- /dev/null +++ b/helpers/middleware/static.go @@ -0,0 +1,18 @@ +package middleware + +import ( + "io/fs" + "net/http" + + "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/filesystem" +) + +func StaticHandler(fs *fs.FS) fiber.Handler { + return filesystem.New(filesystem.Config{ + Root: http.FS(*fs), + Browse: false, + Index: "index.html", + MaxAge: 3600, + }) +} diff --git a/lenz.go b/lenz.go index a24cc73..b19f344 100644 --- a/lenz.go +++ b/lenz.go @@ -10,7 +10,6 @@ import ( "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" @@ -37,16 +36,16 @@ func main() { dir := filepath.Join(cfg.BaseDIR, cfg.GITPath) - gp, err := gitprovider.OpenOrClone(dir, cfg.GitURL, cfg.GitBranch) - + commit, err := gitprovider.OpenOrClone(dir, cfg.GitURL, cfg.GitBranch) if err != nil { 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) + // INFO: the lib, engine and storage objects passed to the server are not made to be recreated. + err = xmlmodels.New(dir, commit.Hash) + if err != nil { + panic(err) + } engine := templating.New(&views.LayoutFS, &views.RoutesFS) storage := memory.New(memory.Config{ diff --git a/templating/engine.go b/templating/engine.go index 404b5ad..4e575c6 100644 --- a/templating/engine.go +++ b/templating/engine.go @@ -5,6 +5,7 @@ import ( "io" "io/fs" "log/slog" + "maps" "net/http" "strconv" "sync" @@ -15,9 +16,8 @@ import ( ) const ( - ASSETS_URL_PREFIX = "/assets" - WS_SERVER = 9000 - RELOAD_TEMPLATE = ` + WS_SERVER = 9000 + RELOAD_TEMPLATE = `