diff --git a/.air.toml b/.air.toml index 21d1da9..a91b9e3 100644 --- a/.air.toml +++ b/.air.toml @@ -4,10 +4,10 @@ tmp_dir = "tmp" [build] args_bin = [] - bin = "./tmp/main" + full_bin = "export KGPZ_WATCH=false; ./tmp/main" cmd = "go build -tags=\"dev\" -o ./tmp/main ." delay = 400 - exclude_dir = ["views/assets", "views/routes", "views/node_modules", "tmp", "vendor", "testdata", "data_git", "cache_gnd", "cache_geonames"] + exclude_dir = ["views/public", "views/node_modules", "tmp", "vendor", "testdata", "data_git", "cache_gnd", "cache_geonames"] exclude_file = [] exclude_regex = ["_test.go"] exclude_unchanged = false diff --git a/config.dev.json b/config.dev.json index c80e62f..9465f5b 100644 --- a/config.dev.json +++ b/config.dev.json @@ -3,5 +3,6 @@ "git_branch": "main", "webhook_endpoint": "/webhook", "webhook_secret": "secret", - "debug": true + "debug": true, + "watch": true } diff --git a/kgpz_web.go b/kgpz_web.go index 224ab9f..24517e6 100644 --- a/kgpz_web.go +++ b/kgpz_web.go @@ -4,6 +4,7 @@ import ( "os" "os/signal" "syscall" + "time" "github.com/Theodor-Springmann-Stiftung/kgpz_web/app" "github.com/Theodor-Springmann-Stiftung/kgpz_web/helpers" @@ -46,15 +47,11 @@ func main() { kgpz := app.NewKGPZ(cfg) kgpz.Init() - engine := templating.NewEngine(&views.LayoutFS, &views.RoutesFS) - engine.Funcs(kgpz) - engine.Globals(fiber.Map{"isDev": cfg.Config.Debug, "name": "KGPZ", "lang": "de"}) - - server := server.Create(kgpz, cfg, engine) - Start(kgpz, server) + server := server.Create(kgpz, cfg, Engine(kgpz, cfg)) + Start(kgpz, server, cfg) } -func Start(k *app.KGPZ, s *server.Server) { +func Start(k *app.KGPZ, s *server.Server, c *providers.ConfigProvider) { s.Start() sigs := make(chan os.Signal, 1) @@ -78,6 +75,33 @@ func Start(k *app.KGPZ, s *server.Server) { done <- true }() + // INFO: hot reloading for poor people + if c.Watch { + go func() { + watcher, err := helpers.NewFileWatcher() + if err != nil { + return + } + + watcher.Append(func(path string) { + logging.Info("File changed: ", path) + time.Sleep(200 * time.Millisecond) + s.Engine(Engine(k, c)) + }) + + err = watcher.RecursiveDir(server.ROUTES_FILEPATH) + if err != nil { + return + } + + err = watcher.RecursiveDir(server.LAYOUT_FILEPATH) + if err != nil { + return + } + }() + + } + // Interactive listening for input // if k.IsDebug() { // go func() { @@ -97,3 +121,10 @@ func Start(k *app.KGPZ, s *server.Server) { // <-done } + +func Engine(kgpz *app.KGPZ, c *providers.ConfigProvider) *templating.Engine { + e := templating.NewEngine(&views.LayoutFS, &views.RoutesFS) + e.Funcs(kgpz) + e.Globals(fiber.Map{"isDev": c.Config.Debug, "name": "KGPZ", "lang": "de"}) + return e +} diff --git a/providers/config.go b/providers/config.go index e588fea..8ede5d9 100644 --- a/providers/config.go +++ b/providers/config.go @@ -21,6 +21,8 @@ const ( DEFAULT_PORT = "8080" DEFAULT_ADDR = "localhost" DEFAULT_HTTPS = false + + ENV_PREFIX = "KGPZ" ) type ConfigProvider struct { @@ -38,6 +40,7 @@ type Config struct { WebHookEndpoint string `json:"webhook_endpoint" envconfig:"WEBHOOK_ENDPOINT"` WebHookSecret string `json:"webhook_secret" envconfig:"WEBHOOK_SECRET"` Debug bool `json:"debug" envconfig:"DEBUG"` + Watch bool `json:"watch" envconfig:"WATCH"` LogData bool `json:"log_data" envconfig:"LOG_DATA"` Address string `json:"address" envconfig:"ADDRESS"` @@ -82,7 +85,7 @@ func readSettingsFile(cfg *Config, path string) *Config { } func readSettingsEnv(cfg *Config) *Config { - _ = envconfig.Process("KGPZ", cfg) + _ = envconfig.Process(ENV_PREFIX, cfg) return cfg } diff --git a/server/server.go b/server/server.go index d671af0..0914507 100644 --- a/server/server.go +++ b/server/server.go @@ -6,7 +6,6 @@ import ( "github.com/Theodor-Springmann-Stiftung/kgpz_web/app" "github.com/Theodor-Springmann-Stiftung/kgpz_web/controllers" - "github.com/Theodor-Springmann-Stiftung/kgpz_web/helpers" "github.com/Theodor-Springmann-Stiftung/kgpz_web/helpers/logging" "github.com/Theodor-Springmann-Stiftung/kgpz_web/providers" "github.com/Theodor-Springmann-Stiftung/kgpz_web/templating" @@ -49,7 +48,6 @@ type Server struct { cache *memory.Storage engine *templating.Engine mu sync.Mutex - watcher *helpers.FileWatcher kgpz *app.KGPZ } @@ -67,39 +65,17 @@ func Create(k *app.KGPZ, c *providers.ConfigProvider, e *templating.Engine) *Ser } } -// INFO: hot reloading for poor people -// BUG: unable to close the old file watcher here, since the process gets aborted in the middle of creating the new one. -func (s *Server) Watcher() error { +func (s *Server) Engine(e *templating.Engine) { + s.Stop() s.mu.Lock() - defer s.mu.Unlock() - - watcher, err := helpers.NewFileWatcher() - if err != nil { - return err - } - - s.watcher = watcher - s.watcher.Append(func(path string) { - logging.Info("File changed: ", path) - time.Sleep(200 * time.Millisecond) - s.Restart() - }) - - err = s.watcher.RecursiveDir(ROUTES_FILEPATH) - if err != nil { - return err - } - - err = s.watcher.RecursiveDir(LAYOUT_FILEPATH) - if err != nil { - return err - } - - return nil + s.engine = e + s.mu.Unlock() + s.Start() } func (s *Server) Start() { - s.engine.Reload() + s.mu.Lock() + defer s.mu.Unlock() if s.cache == nil { s.cache = memory.New(memory.Config{ @@ -174,10 +150,6 @@ func (s *Server) Start() { s.runner(srv) - if s.Config.Debug { - err := s.Watcher() - logging.Error(err, "Error setting up file watcher") - } } func (s *Server) Stop() { diff --git a/views/routes/issue/components/_inhaltsverzeichnis.gohtml b/views/routes/issue/components/_inhaltsverzeichnis.gohtml index 25e9312..b68b533 100644 --- a/views/routes/issue/components/_inhaltsverzeichnis.gohtml +++ b/views/routes/issue/components/_inhaltsverzeichnis.gohtml @@ -16,7 +16,7 @@ {{ if gt (len $piece.IssueRefs) 1 }}