diff --git a/cmd/playground/main.go b/cmd/playground/main.go index 7dcc634..5e3147f 100644 --- a/cmd/playground/main.go +++ b/cmd/playground/main.go @@ -55,9 +55,9 @@ Access the web UI at http://localhost:8080/playground (or your configured host:p Run: func(cmd *cobra.Command, args []string) { server := playground.NewServerWithRefresh(port, host, refreshCache) - // Handle interrupt signals (Ctrl+C, Ctrl+Z, SIGTERM) + // Handle interrupt signals sigChan := make(chan os.Signal, 1) - signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM, syscall.SIGTSTP) + setupSignalHandling(sigChan) // Start server in goroutine errChan := make(chan error, 1) @@ -70,12 +70,7 @@ Access the web UI at http://localhost:8080/playground (or your configured host:p // Wait for interrupt or error select { case sig := <-sigChan: - // Handle SIGTSTP (Ctrl+Z) - convert to graceful shutdown - if sig == syscall.SIGTSTP { - color.Yellow("\nšŸ›‘ Received suspend signal (Ctrl+Z), shutting down gracefully...") - } else { - color.Yellow("\nšŸ›‘ Received interrupt signal, shutting down...") - } + handleShutdownSignal(sig) shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second) defer shutdownCancel() @@ -138,3 +133,22 @@ func createRefreshCmd() *cobra.Command { } } +// Platform-specific signal handling +func setupSignalHandling(sigChan chan<- os.Signal) { + // Windows doesn't have SIGTSTP, so we use a conditional build + signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM) + // On Unix systems, SIGTSTP is also handled (see signal_unix.go) + notifyUNIXSignals(sigChan) +} + +func handleShutdownSignal(sig os.Signal) { + msg := getShutdownMessage(sig) + color.Yellow("\nšŸ›‘ %s", msg) +} + +// Stub functions - overridden on Unix systems +var notifyUNIXSignals = func(sigChan chan<- os.Signal) {} +var getShutdownMessage = func(sig os.Signal) string { + return "Received interrupt signal, shutting down..." +} + diff --git a/cmd/playground/signal_unix.go b/cmd/playground/signal_unix.go new file mode 100644 index 0000000..aeb7053 --- /dev/null +++ b/cmd/playground/signal_unix.go @@ -0,0 +1,23 @@ +// +build unix + +package main + +import ( + "os" + "os/signal" + "syscall" +) + +func init() { + // Override the Unix signal handling + notifyUNIXSignals = func(sigChan chan<- os.Signal) { + signal.Notify(sigChan, syscall.SIGTSTP) + } + + getShutdownMessage = func(sig os.Signal) string { + if sig == syscall.SIGTSTP { + return "Received suspend signal (Ctrl+Z), shutting down gracefully..." + } + return "Received interrupt signal, shutting down..." + } +}