feat: watch and live reload in development
1 file changed, 21 insertions(+), 3 deletions(-)
changed files
M internal/server/server.go → internal/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