Skip to content

Commit

Permalink
add signal handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sethgrid committed Dec 2, 2024
1 parent 43b9f17 commit 6c78906
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"net"
"net/http"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"

"github.com/go-chi/chi/v5"
Expand Down Expand Up @@ -327,6 +329,18 @@ func (s *Server) Serve() error {
s.publicHTTPServer = &publicHTTP
s.mu.Unlock()

// Graceful shutdown on signals
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)

go func() {
<-quit
s.parentLogger.Info("shutdown signal received, shutting down gracefully...")
if err := s.Close(); err != nil {
s.parentLogger.Error("error during shutdown", "error", err.Error())
}
}()

// blocking
if err := publicHTTP.Serve(listener); err != nil {
// capturing server error for easier debugging during testing
Expand Down

0 comments on commit 6c78906

Please sign in to comment.