FileWatcher redone

This commit is contained in:
Simon Martens
2024-12-04 14:05:35 +01:00
parent 09d900b5b3
commit a39d6be822
7 changed files with 63 additions and 54 deletions

View File

@@ -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

View File

@@ -3,5 +3,6 @@
"git_branch": "main",
"webhook_endpoint": "/webhook",
"webhook_secret": "secret",
"debug": true
"debug": true,
"watch": true
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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() {

View File

@@ -16,7 +16,7 @@
<!-- Links zu anderen Teilen: -->
{{ if gt (len $piece.IssueRefs) 1 }}
<div>
{{ len $piece.IssueRefs }} Teile
{{ len $piece.IssueRefs }} Teile:
<ol>
{{ range $issue := $piece.IssueRefs }}
<li>

View File

@@ -3,15 +3,17 @@ Eintrag!
<!-- Autor(en) -->
{{ $authorset := false }}
{{ range $agentref := $piece.AgentRefs }}
<div class="authors">
{{ range $agentref := $piece.AgentRefs }}
{{ if (or (eq $agentref.Category "") (eq $agentref.Category "autor")) }}
{{ $agent := GetAgent $agentref.Ref }}
<!-- NOT IMPLEMENTED: Multiple agent names -->
{{- if gt (len $agent.Names) 0 -}}
<div class="inline-block">{{- index $agent.Names 0 -}}</div>
<div class="author inline-block">{{- index $agent.Names 0 -}}</div>
{{ end }}
{{ end }}
{{ end }}
{{ end }}
</div>
{{ range $annotation := $piece.AnnotationNote.Annotations }}
<div>