From 8411fb4a3acebe46acaf7a2ff6c4e58018737d65 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Fri, 3 May 2024 00:10:23 +0200 Subject: feat: watch and live reload in development --- internal/server/dev.go | 71 +++++++++++++++++++++++++++++++++++++++++++++++ internal/server/server.go | 24 ++++++++++++++-- 2 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 internal/server/dev.go (limited to 'internal') diff --git a/internal/server/dev.go b/internal/server/dev.go new file mode 100644 index 0000000..52ab29b --- /dev/null +++ b/internal/server/dev.go @@ -0,0 +1,71 @@ +package server + +import ( + "fmt" + "io/fs" + "log/slog" + "os" + "path/filepath" + "time" + + "github.com/fsnotify/fsnotify" + "github.com/pkg/errors" +) + +type FileWatcher struct { + *fsnotify.Watcher +} + +func NewFileWatcher() (*FileWatcher, error) { + watcher, err := fsnotify.NewWatcher() + if err != nil { + return nil, errors.WithMessage(err, "could not create watcher") + } + + return &FileWatcher{watcher}, nil +} + +func (watcher FileWatcher) AddRecursive(from string) error { + slog.Debug(fmt.Sprintf("watching files under %s", from)) + err := filepath.WalkDir(from, func(path string, entry fs.DirEntry, err error) error { + if err != nil { + return errors.WithMessagef(err, "could not walk directory %s", path) + } + if entry.IsDir() { + slog.Debug(fmt.Sprintf("adding directory %s to watcher", path)) + if err = watcher.Add(path); err != nil { + return errors.WithMessagef(err, "could not add directory %s to watcher", path) + } + } + + return nil + }) + + return errors.WithMessage(err, "error walking directory tree") +} + +func (watcher FileWatcher) Start(callback func()) { + for { + select { + case event := <-watcher.Events: + slog.Debug(fmt.Sprintf("event received: %s", event)) + if event.Has(fsnotify.Create) || event.Has(fsnotify.Rename) { + f, err := os.Stat(event.Name) + if err != nil { + slog.Error(fmt.Sprintf("error handling %s event: %v", event.Op.String(), err)) + } else if f.IsDir() { + err = watcher.Add(event.Name) + if err != nil { + slog.Error(fmt.Sprintf("error adding new folder to watcher: %v", err)) + } + } + } + if event.Has(fsnotify.Rename) || event.Has(fsnotify.Write) { + callback() + time.Sleep(500 * time.Millisecond) + } + case err := <-watcher.Errors: + slog.Error(fmt.Sprintf("error in watcher: %v", err)) + } + } +} diff --git a/internal/server/server.go b/internal/server/server.go index fc56d47..04ee94c 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -2,6 +2,7 @@ package server import ( "context" + "fmt" "html/template" "io" "log" @@ -87,7 +88,8 @@ func New(runtimeConfig *Config) (*Server, error) { Repanic: true, }) - tpl := template.Must(template.ParseGlob(path.Join("frontend", "templates", "*.tmpl"))) + templatePaths := path.Join("frontend", "templates", "*.tmpl") + tpl := template.Must(template.ParseGlob(templatePaths)) top := http.NewServeMux() mux := http.NewServeMux() @@ -108,8 +110,24 @@ func New(runtimeConfig *Config) (*Server, error) { applyDevModeOverrides(config) liveReload := livereload.New() liveReload.Start() - mux.Handle("/livereload", liveReload) - // liveReload.Reload() + top.Handle("/livereload", liveReload) + fw, err := NewFileWatcher() + if err != nil { + return nil, errors.WithMessage(err, "could not create file watcher") + } + err = fw.AddRecursive("frontend") + if err != nil { + return nil, errors.WithMessage(err, "could not add directory to file watcher") + } + go fw.Start(func() { + t, err := template.ParseGlob(path.Join("frontend", "templates", "*.tmpl")) + if err != nil { + slog.Error(fmt.Sprintf("could not parse template: %v", err)) + } else { + tpl = t + liveReload.Reload() + } + }) } var logWriter io.Writer -- cgit 1.4.1