about summary refs log tree commit diff stats
path: root/internal/server/server.go
diff options
context:
space:
mode:
authorAlan Pearce2024-05-03 00:10:23 +0200
committerAlan Pearce2024-05-03 00:13:51 +0200
commit8411fb4a3acebe46acaf7a2ff6c4e58018737d65 (patch)
tree17ea0df23a5b393c42f3e9fb003dc7ef1b27ecf0 /internal/server/server.go
parent73603079e29bc89c54296a9e12b5a779cd84c023 (diff)
downloadsearchix-8411fb4a3acebe46acaf7a2ff6c4e58018737d65.tar.lz
searchix-8411fb4a3acebe46acaf7a2ff6c4e58018737d65.tar.zst
searchix-8411fb4a3acebe46acaf7a2ff6c4e58018737d65.zip
feat: watch and live reload in development
Diffstat (limited to 'internal/server/server.go')
-rw-r--r--internal/server/server.go24
1 files changed, 21 insertions, 3 deletions
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