diff options
Diffstat (limited to 'cmd/dev/main.go')
-rw-r--r-- | cmd/dev/main.go | 86 |
1 files changed, 43 insertions, 43 deletions
diff --git a/cmd/dev/main.go b/cmd/dev/main.go index aa3102f..459cfaf 100644 --- a/cmd/dev/main.go +++ b/cmd/dev/main.go @@ -5,8 +5,6 @@ import ( "fmt" "io" "io/fs" - "log" - "log/slog" "net/http" "net/http/httputil" @@ -15,12 +13,12 @@ import ( "os/signal" "path" "path/filepath" - "strings" "sync" "syscall" "time" "website/internal/config" + "website/internal/log" "github.com/antage/eventsource" "github.com/ardanlabs/conf/v3" @@ -41,14 +39,14 @@ func RunCommandPiped( command string, args ...string, ) (cmd *exec.Cmd, err error) { - slog.Debug(fmt.Sprintf("running command %s %s", command, strings.Join(args, " "))) + log.Debug("running command", "command", command, "args", args) cmd = exec.CommandContext(ctx, command, args...) cmd.Env = append(os.Environ(), "DEBUG=") cmd.Cancel = func() error { - slog.Debug("signalling child") + log.Debug("signalling child") err := cmd.Process.Signal(os.Interrupt) if err != nil { - slog.Error(fmt.Sprintf("signal error: %v", err)) + log.Error("signal error:", "error", err) } return err } @@ -80,12 +78,12 @@ func NewFileWatcher(pollTime time.Duration) (*FileWatcher, error) { } func (watcher FileWatcher) WatchAllFiles(from string) error { - slog.Debug(fmt.Sprintf("watching files under %s", from)) + log.Debug("watching files under", "from", from) err := filepath.Walk(from, func(path string, info fs.FileInfo, err error) error { if err != nil { return err } - // slog.Debug(fmt.Sprintf("adding file %s to watcher", path)) + // log.Debug(fmt.Sprintf("adding file %s to watcher", path)) if err = watcher.Add(path); err != nil { return err } @@ -107,7 +105,11 @@ func build(ctx context.Context, config DevConfig) error { } err = cmd.Run() - slog.Debug(fmt.Sprintf("build command exited with code %d", cmd.ProcessState.ExitCode())) + log.Debug( + "build command exited", + "status", + cmd.ProcessState.ExitCode(), + ) if err != nil { return errors.WithMessage(err, "error running build command") } @@ -148,7 +150,7 @@ func server(ctx context.Context, devConfig DevConfig) error { for { select { case <-ctx.Done(): - slog.Debug("server context done") + log.Debug("server context done") err := cmd.Process.Signal(os.Interrupt) if err != nil { return err @@ -161,10 +163,8 @@ func server(ctx context.Context, devConfig DevConfig) error { } func main() { - if os.Getenv("DEBUG") != "" { - slog.SetLogLoggerLevel(slog.LevelDebug) - } var wg sync.WaitGroup + log.Configure(false) devConfig := DevConfig{} help, err := conf.Parse("", &devConfig) @@ -173,23 +173,23 @@ func main() { fmt.Println(help) os.Exit(1) } - log.Panicf("parsing dev configuration: %v", err) + log.Panic("parsing dev configuration", "error", err) } - slog.Debug(fmt.Sprintf("using folder %s for build output", devConfig.TempDir)) + log.Debug("running with in /tmp", "dir", devConfig.TempDir) ctx, cancel := context.WithCancel(context.Background()) defer cancel() - slog.Debug("setting interrupt handler") + log.Debug("setting interrupt handler") c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, syscall.SIGTERM) go func() { sig := <-c - slog.Info(fmt.Sprintf("shutting down on signal %d", sig)) + log.Info("shutting down on signal", "sig", sig) cancel() sig = <-c - slog.Info(fmt.Sprintf("got second signal, dying %d", sig)) + log.Info("got second signal, dying", "sig", sig) os.Exit(1) }() @@ -205,9 +205,9 @@ func main() { go func() { defer wg.Done() defer devCancel() - slog.Debug("waiting for first server launch") + log.Debug("waiting for first server launch") <-serverChan - slog.Debug("got first server launch event") + log.Debug("got first server launch event") http.Handle("/", &httputil.ReverseProxy{ Rewrite: func(req *httputil.ProxyRequest) { @@ -220,7 +220,7 @@ func main() { go func() { err := srv.ListenAndServe() if err != nil && err != http.ErrServerClosed { - slog.Error(err.Error()) + log.Error(err.Error()) cancel() } done <- true @@ -228,29 +228,29 @@ func main() { go func() { for ready := range serverChan { if ready { - slog.Debug("sending reload message") + log.Debug("sending reload message") eventsource.SendEventMessage("reload", "", "") } else { - slog.Debug("server not ready") + log.Debug("server not ready") } } }() - slog.Info(fmt.Sprintf("dev server listening on %s", devConfig.BaseURL.Host)) + log.Info("dev server listening on", "host", devConfig.BaseURL.String()) <-done - slog.Debug("dev server closed") + log.Debug("dev server closed") }() fw, err := NewFileWatcher(500 * time.Millisecond) if err != nil { - log.Fatalf("error creating file watcher: %v", err) + log.Fatal("error creating file watcher", "error", err) } err = fw.WatchAllFiles("content") if err != nil { - log.Fatalf("could not watch files in content directory: %v", err) + log.Fatal("could not watch files in content directory", "error", err) } err = fw.WatchAllFiles("templates") if err != nil { - log.Fatalf("could not watch files in templates directory: %v", err) + log.Fatal("could not watch files in templates directory", "error", err) } var exitCode int @@ -258,14 +258,14 @@ func main() { loop: for { serverCtx, stopServer := context.WithCancel(ctx) - slog.Debug("starting build") + log.Debug("starting build") err := build(ctx, devConfig) if err != nil { - slog.Error(fmt.Sprintf("build error: %v", err)) + log.Error("build error:", "error", err) // don't set up the server until there's a FS change event } else { - slog.Debug("setting up server") + log.Debug("setting up server") wg.Add(1) go func() { defer wg.Done() @@ -276,43 +276,43 @@ loop: select { case <-ctx.Done(): - slog.Debug("main context cancelled") - slog.Debug("calling server shutdown") + log.Debug("main context cancelled") + log.Debug("calling server shutdown") err := srv.Shutdown(devCtx) if err != nil { - slog.Debug("shutdown error", "error", err) + log.Debug("shutdown error", "error", err) } exitCode = 1 break loop case event := <-fw.Events: - slog.Debug(fmt.Sprintf("event received: %v", event)) + log.Debug("event received:", "event", event) stopServer() serverChan <- false - slog.Debug("waiting for server shutdown") + log.Debug("waiting for server shutdown") <-serverErr - slog.Debug("server shutdown completed") + log.Debug("server shutdown completed") continue case err = <-serverErr: if err != nil && err != context.Canceled { var exerr *exec.ExitError - slog.Error(fmt.Sprintf("server reported error: %v", err)) + log.Error("server reported error:", "error", err) if errors.As(err, &exerr) { - slog.Debug("server exit error") + log.Debug("server exit error") } else { - slog.Debug("server other error") + log.Debug("server other error") } break } - slog.Debug("no error or server context cancelled") + log.Debug("no error or server context cancelled") continue } - slog.Debug("waiting on server") + log.Debug("waiting on server") exitCode = 0 break } - slog.Debug("waiting for wg before shutting down") + log.Debug("waiting for wg before shutting down") wg.Wait() os.Exit(exitCode) } |