- 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.
65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"DanceLessonsCoach/pkg/server"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
func main() {
|
|
// Create root context with cancellation
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
// 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")
|
|
} |