mirror of
https://github.com/Theodor-Springmann-Stiftung/kgpz_web.git
synced 2025-10-29 09:05:30 +00:00
FileWatcher redone
This commit is contained in:
@@ -4,10 +4,10 @@ tmp_dir = "tmp"
|
|||||||
|
|
||||||
[build]
|
[build]
|
||||||
args_bin = []
|
args_bin = []
|
||||||
bin = "./tmp/main"
|
full_bin = "export KGPZ_WATCH=false; ./tmp/main"
|
||||||
cmd = "go build -tags=\"dev\" -o ./tmp/main ."
|
cmd = "go build -tags=\"dev\" -o ./tmp/main ."
|
||||||
delay = 400
|
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_file = []
|
||||||
exclude_regex = ["_test.go"]
|
exclude_regex = ["_test.go"]
|
||||||
exclude_unchanged = false
|
exclude_unchanged = false
|
||||||
|
|||||||
@@ -3,5 +3,6 @@
|
|||||||
"git_branch": "main",
|
"git_branch": "main",
|
||||||
"webhook_endpoint": "/webhook",
|
"webhook_endpoint": "/webhook",
|
||||||
"webhook_secret": "secret",
|
"webhook_secret": "secret",
|
||||||
"debug": true
|
"debug": true,
|
||||||
|
"watch": true
|
||||||
}
|
}
|
||||||
|
|||||||
45
kgpz_web.go
45
kgpz_web.go
@@ -4,6 +4,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/Theodor-Springmann-Stiftung/kgpz_web/app"
|
"github.com/Theodor-Springmann-Stiftung/kgpz_web/app"
|
||||||
"github.com/Theodor-Springmann-Stiftung/kgpz_web/helpers"
|
"github.com/Theodor-Springmann-Stiftung/kgpz_web/helpers"
|
||||||
@@ -46,15 +47,11 @@ func main() {
|
|||||||
kgpz := app.NewKGPZ(cfg)
|
kgpz := app.NewKGPZ(cfg)
|
||||||
kgpz.Init()
|
kgpz.Init()
|
||||||
|
|
||||||
engine := templating.NewEngine(&views.LayoutFS, &views.RoutesFS)
|
server := server.Create(kgpz, cfg, Engine(kgpz, cfg))
|
||||||
engine.Funcs(kgpz)
|
Start(kgpz, server, cfg)
|
||||||
engine.Globals(fiber.Map{"isDev": cfg.Config.Debug, "name": "KGPZ", "lang": "de"})
|
|
||||||
|
|
||||||
server := server.Create(kgpz, cfg, engine)
|
|
||||||
Start(kgpz, server)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Start(k *app.KGPZ, s *server.Server) {
|
func Start(k *app.KGPZ, s *server.Server, c *providers.ConfigProvider) {
|
||||||
s.Start()
|
s.Start()
|
||||||
|
|
||||||
sigs := make(chan os.Signal, 1)
|
sigs := make(chan os.Signal, 1)
|
||||||
@@ -78,6 +75,33 @@ func Start(k *app.KGPZ, s *server.Server) {
|
|||||||
done <- true
|
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
|
// Interactive listening for input
|
||||||
// if k.IsDebug() {
|
// if k.IsDebug() {
|
||||||
// go func() {
|
// go func() {
|
||||||
@@ -97,3 +121,10 @@ func Start(k *app.KGPZ, s *server.Server) {
|
|||||||
//
|
//
|
||||||
<-done
|
<-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
|
||||||
|
}
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ const (
|
|||||||
DEFAULT_PORT = "8080"
|
DEFAULT_PORT = "8080"
|
||||||
DEFAULT_ADDR = "localhost"
|
DEFAULT_ADDR = "localhost"
|
||||||
DEFAULT_HTTPS = false
|
DEFAULT_HTTPS = false
|
||||||
|
|
||||||
|
ENV_PREFIX = "KGPZ"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ConfigProvider struct {
|
type ConfigProvider struct {
|
||||||
@@ -38,6 +40,7 @@ type Config struct {
|
|||||||
WebHookEndpoint string `json:"webhook_endpoint" envconfig:"WEBHOOK_ENDPOINT"`
|
WebHookEndpoint string `json:"webhook_endpoint" envconfig:"WEBHOOK_ENDPOINT"`
|
||||||
WebHookSecret string `json:"webhook_secret" envconfig:"WEBHOOK_SECRET"`
|
WebHookSecret string `json:"webhook_secret" envconfig:"WEBHOOK_SECRET"`
|
||||||
Debug bool `json:"debug" envconfig:"DEBUG"`
|
Debug bool `json:"debug" envconfig:"DEBUG"`
|
||||||
|
Watch bool `json:"watch" envconfig:"WATCH"`
|
||||||
LogData bool `json:"log_data" envconfig:"LOG_DATA"`
|
LogData bool `json:"log_data" envconfig:"LOG_DATA"`
|
||||||
|
|
||||||
Address string `json:"address" envconfig:"ADDRESS"`
|
Address string `json:"address" envconfig:"ADDRESS"`
|
||||||
@@ -82,7 +85,7 @@ func readSettingsFile(cfg *Config, path string) *Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func readSettingsEnv(cfg *Config) *Config {
|
func readSettingsEnv(cfg *Config) *Config {
|
||||||
_ = envconfig.Process("KGPZ", cfg)
|
_ = envconfig.Process(ENV_PREFIX, cfg)
|
||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
|
|
||||||
"github.com/Theodor-Springmann-Stiftung/kgpz_web/app"
|
"github.com/Theodor-Springmann-Stiftung/kgpz_web/app"
|
||||||
"github.com/Theodor-Springmann-Stiftung/kgpz_web/controllers"
|
"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/helpers/logging"
|
||||||
"github.com/Theodor-Springmann-Stiftung/kgpz_web/providers"
|
"github.com/Theodor-Springmann-Stiftung/kgpz_web/providers"
|
||||||
"github.com/Theodor-Springmann-Stiftung/kgpz_web/templating"
|
"github.com/Theodor-Springmann-Stiftung/kgpz_web/templating"
|
||||||
@@ -49,7 +48,6 @@ type Server struct {
|
|||||||
cache *memory.Storage
|
cache *memory.Storage
|
||||||
engine *templating.Engine
|
engine *templating.Engine
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
watcher *helpers.FileWatcher
|
|
||||||
|
|
||||||
kgpz *app.KGPZ
|
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
|
func (s *Server) Engine(e *templating.Engine) {
|
||||||
// BUG: unable to close the old file watcher here, since the process gets aborted in the middle of creating the new one.
|
s.Stop()
|
||||||
func (s *Server) Watcher() error {
|
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
s.engine = e
|
||||||
|
s.mu.Unlock()
|
||||||
watcher, err := helpers.NewFileWatcher()
|
s.Start()
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Start() {
|
func (s *Server) Start() {
|
||||||
s.engine.Reload()
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
if s.cache == nil {
|
if s.cache == nil {
|
||||||
s.cache = memory.New(memory.Config{
|
s.cache = memory.New(memory.Config{
|
||||||
@@ -174,10 +150,6 @@ func (s *Server) Start() {
|
|||||||
|
|
||||||
s.runner(srv)
|
s.runner(srv)
|
||||||
|
|
||||||
if s.Config.Debug {
|
|
||||||
err := s.Watcher()
|
|
||||||
logging.Error(err, "Error setting up file watcher")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Stop() {
|
func (s *Server) Stop() {
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
<!-- Links zu anderen Teilen: -->
|
<!-- Links zu anderen Teilen: -->
|
||||||
{{ if gt (len $piece.IssueRefs) 1 }}
|
{{ if gt (len $piece.IssueRefs) 1 }}
|
||||||
<div>
|
<div>
|
||||||
{{ len $piece.IssueRefs }} Teile
|
{{ len $piece.IssueRefs }} Teile:
|
||||||
<ol>
|
<ol>
|
||||||
{{ range $issue := $piece.IssueRefs }}
|
{{ range $issue := $piece.IssueRefs }}
|
||||||
<li>
|
<li>
|
||||||
|
|||||||
@@ -3,15 +3,17 @@ Eintrag!
|
|||||||
|
|
||||||
<!-- Autor(en) -->
|
<!-- Autor(en) -->
|
||||||
{{ $authorset := false }}
|
{{ $authorset := false }}
|
||||||
{{ range $agentref := $piece.AgentRefs }}
|
<div class="authors">
|
||||||
{{ if (or (eq $agentref.Category "") (eq $agentref.Category "autor")) }}
|
{{ range $agentref := $piece.AgentRefs }}
|
||||||
{{ $agent := GetAgent $agentref.Ref }}
|
{{ if (or (eq $agentref.Category "") (eq $agentref.Category "autor")) }}
|
||||||
<!-- NOT IMPLEMENTED: Multiple agent names -->
|
{{ $agent := GetAgent $agentref.Ref }}
|
||||||
{{- if gt (len $agent.Names) 0 -}}
|
<!-- NOT IMPLEMENTED: Multiple agent names -->
|
||||||
<div class="inline-block">{{- index $agent.Names 0 -}}</div>
|
{{- if gt (len $agent.Names) 0 -}}
|
||||||
|
<div class="author inline-block">{{- index $agent.Names 0 -}}</div>
|
||||||
|
{{ end }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ end }}
|
</div>
|
||||||
|
|
||||||
{{ range $annotation := $piece.AnnotationNote.Annotations }}
|
{{ range $annotation := $piece.AnnotationNote.Annotations }}
|
||||||
<div>
|
<div>
|
||||||
|
|||||||
Reference in New Issue
Block a user