diff --git a/.air.toml b/.air.toml new file mode 100644 index 0000000..ea2f725 --- /dev/null +++ b/.air.toml @@ -0,0 +1,63 @@ +root = "." +testdata_dir = "testdata" +tmp_dir = "tmp" + +[build] +args_bin = [] +full_bin = "./tmp/lenz" +cmd = "go build -tags=dev,fts5,sqlite_icu -o ./tmp/lenz ." +delay = 400 +exclude_dir = [ + "import", + "views", + "tmp", + "vendor", + "testdata", + "_cache", + "data_git", + "cache_gnd", + "cache_geonames", + "pb_data", + "Almanach-Bilder", + "Static-Bilder", +] +exclude_file = [] +exclude_regex = ["_test.go"] +exclude_unchanged = false +follow_symlink = false +include_dir = [] +include_ext = ["go", "tpl", "tmpl", "html", "gohtml", "js", "css", "xsl"] +include_file = [] +kill_delay = "0s" +log = "build-errors.log" +poll = false +poll_interval = 0 +post_cmd = [] +pre_cmd = [""] +rerun = false +rerun_delay = 250 +send_interrupt = true +stop_on_error = true + +[color] +app = "" +build = "yellow" +main = "magenta" +runner = "green" +watcher = "cyan" + +[log] +main_only = false +time = false + +[misc] +clean_on_exit = true + +[proxy] +app_port = 8090 +enabled = true +proxy_port = 8081 + +[screen] +clear_on_rebuild = true +keep_scroll = true diff --git a/.gitignore b/.gitignore index e934adf..120a9cd 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ cache/ +_cache/ +tmp/ diff --git a/lenz.go b/lenz.go index 3d419c6..a24cc73 100644 --- a/lenz.go +++ b/lenz.go @@ -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() } diff --git a/templating/engine.go b/templating/engine.go index 9f8f6c6..404b5ad 100644 --- a/templating/engine.go +++ b/templating/engine.go @@ -4,8 +4,9 @@ import ( "html/template" "io" "io/fs" - "log" + "log/slog" "net/http" + "strconv" "sync" "github.com/Theodor-Springmann-Stiftung/lenz-web/helpers/functions" @@ -15,6 +16,7 @@ import ( const ( ASSETS_URL_PREFIX = "/assets" + WS_SERVER = 9000 RELOAD_TEMPLATE = `