about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2024-05-05 18:05:30 +0200
committerAlan Pearce2024-05-05 18:05:30 +0200
commit158904f480e558ca00f680e7c577bb6329605eff (patch)
tree5e475f3dec8561c03c718ba815893ef3854dfbe1
parentc689cf5d0c8f6c62fb21c300cec187305d8262d9 (diff)
downloadsearchix-158904f480e558ca00f680e7c577bb6329605eff.tar.lz
searchix-158904f480e558ca00f680e7c577bb6329605eff.tar.zst
searchix-158904f480e558ca00f680e7c577bb6329605eff.zip
refactor: only reload templates when such files are updated
-rw-r--r--internal/server/dev.go4
-rw-r--r--internal/server/server.go11
2 files changed, 9 insertions, 6 deletions
diff --git a/internal/server/dev.go b/internal/server/dev.go
index 52ab29b..1318801 100644
--- a/internal/server/dev.go
+++ b/internal/server/dev.go
@@ -44,7 +44,7 @@ func (watcher FileWatcher) AddRecursive(from string) error {
 	return errors.WithMessage(err, "error walking directory tree")
 }
 
-func (watcher FileWatcher) Start(callback func()) {
+func (watcher FileWatcher) Start(callback func(string)) {
 	for {
 		select {
 		case event := <-watcher.Events:
@@ -61,7 +61,7 @@ func (watcher FileWatcher) Start(callback func()) {
 				}
 			}
 			if event.Has(fsnotify.Rename) || event.Has(fsnotify.Write) {
-				callback()
+				callback(event.Name)
 				time.Sleep(500 * time.Millisecond)
 			}
 		case err := <-watcher.Errors:
diff --git a/internal/server/server.go b/internal/server/server.go
index aa37674..02b39a0 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -203,10 +203,13 @@ func New(runtimeConfig *Config) (*Server, error) {
 		if err != nil {
 			return nil, errors.WithMessage(err, "could not add directory to file watcher")
 		}
-		go fw.Start(func() {
-			templates, err = loadTemplates()
-			if err != nil {
-				slog.Error(fmt.Sprintf("could not reload templates: %v", err))
+		go fw.Start(func(filename string) {
+			slog.Debug(fmt.Sprintf("got filename %s", filename))
+			if path.Ext(filename) == ".gotmpl" {
+				templates, err = loadTemplates()
+				if err != nil {
+					slog.Error(fmt.Sprintf("could not reload templates: %v", err))
+				}
 			}
 			liveReload.Reload()
 		})