Enhance server with context initialization and graceful shutdown
- Added context-aware server initialization in cmd/server/main.go - Implemented graceful shutdown handling with SIGINT/SIGTERM signals - Added 30-second shutdown timeout for active connections - Updated Greet service to use context.Context as first parameter - Enhanced Zerolog integration with Trace level logging - Added context-aware logging in Greet function calls - Fixed route structure to use /api/v1/greet/* prefix - Updated all handlers and tests to use context - Comprehensive AGENTS.md documentation with verified commands - Added server context management architecture section - Updated API endpoint documentation with working examples Changes: - cmd/server/main.go: Complete rewrite with context and graceful shutdown - pkg/greet/greet.go: Added context parameter and trace logging - pkg/greet/api_v1.go: Updated interface and handlers for context - pkg/greet/greet_test.go: Updated tests to use context - cmd/greet/main.go: Updated CLI to use context - pkg/server/server.go: Trace level config and context logging - AGENTS.md: Comprehensive documentation update - go.mod/go.sum: Added Zerolog dependency All tests passing, server working with graceful shutdown verified.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
@@ -14,5 +15,5 @@ func main() {
|
||||
name = os.Args[1]
|
||||
}
|
||||
|
||||
fmt.Println(service.Greet(name))
|
||||
fmt.Println(service.Greet(context.Background(), name))
|
||||
}
|
||||
@@ -1,15 +1,65 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"DanceLessonsCoach/pkg/server"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
server := server.NewServer()
|
||||
// Create root context with cancellation
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
fmt.Println("Server running on :8080")
|
||||
http.ListenAndServe(":8080", server.Router())
|
||||
// Set up graceful shutdown
|
||||
sigChan := make(chan os.Signal, 1)
|
||||
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
// Start server in goroutine
|
||||
server := server.NewServer()
|
||||
serverCtx, serverStop := context.WithCancel(ctx)
|
||||
|
||||
go func() {
|
||||
fmt.Println("Server running on :8080")
|
||||
log.Info().Msg("Starting HTTP server on :8080")
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: ":8080",
|
||||
Handler: server.Router(),
|
||||
}
|
||||
|
||||
// Listen for shutdown signal
|
||||
go func() {
|
||||
<-sigChan
|
||||
log.Info().Msg("Shutdown signal received")
|
||||
|
||||
// Create shutdown context with timeout
|
||||
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer shutdownCancel()
|
||||
|
||||
if err := srv.Shutdown(shutdownCtx); err != nil {
|
||||
log.Error().Err(err).Msg("Server shutdown failed")
|
||||
} else {
|
||||
log.Info().Msg("Server shutdown complete")
|
||||
}
|
||||
|
||||
cancel()
|
||||
serverStop()
|
||||
}()
|
||||
|
||||
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
log.Error().Err(err).Msg("Server error")
|
||||
}
|
||||
}()
|
||||
|
||||
// Wait for shutdown
|
||||
<-serverCtx.Done()
|
||||
log.Info().Msg("Server exited")
|
||||
}
|
||||
Reference in New Issue
Block a user