all repos — searchix @ 8411fb4a3acebe46acaf7a2ff6c4e58018737d65

Search engine for NixOS, nix-darwin, home-manager and NUR users

feat: watch and live reload in development

Alan Pearce
commit

8411fb4a3acebe46acaf7a2ff6c4e58018737d65

parent

73603079e29bc89c54296a9e12b5a779cd84c023

1 file changed, 21 insertions(+), 3 deletions(-)

changed files
M internal/server/server.gointernal/server/server.go
@@ -2,6 +2,7 @@ package server
import ( "context" + "fmt" "html/template" "io" "log"
@@ -87,7 +88,8 @@ sentryHandler := sentryhttp.New(sentryhttp.Options{
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 @@ if runtimeConfig.LiveReload {
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