package main import ( "context" "os/signal" "syscall" "time" "music-server/internal/logging" "music-server/internal/server" "go.uber.org/zap" ) const shutdownTimeout = 5 * time.Second func main() { ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) defer stop() s := server.NewServerInstance() serveErr := make(chan error, 1) go func() { serveErr <- s.ListenAndServe() }() select { case err := <-serveErr: if err != nil { logging.GetLogger().Fatal("HTTP server error", zap.Error(err)) } case <-ctx.Done(): stop() logging.GetLogger().Info("Shutting down gracefully, press Ctrl+C again to force") } shutdownCtx, cancel := context.WithTimeout(context.Background(), shutdownTimeout) defer cancel() if err := s.Shutdown(shutdownCtx); err != nil { logging.GetLogger().Error("Server forced to shutdown with error", zap.Error(err)) } logging.GetLogger().Info("Graceful shutdown complete") }