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,14 +1,16 @@
|
||||
package greet
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type Greeter interface {
|
||||
Greet(name string) string
|
||||
Greet(ctx context.Context, name string) string
|
||||
}
|
||||
|
||||
type ApiV1Greet interface {
|
||||
@@ -24,18 +26,20 @@ func NewApiV1GreetHandler(greeter Greeter) ApiV1Greet {
|
||||
}
|
||||
|
||||
func (h *apiV1GreetHandler) RegisterRoutes(router chi.Router) {
|
||||
log.Trace().Msg("Registering greet routes")
|
||||
router.Get("/", h.handleGreetQuery)
|
||||
router.Get("/{name}", h.handleGreetPath)
|
||||
log.Trace().Msg("Greet routes registered")
|
||||
}
|
||||
|
||||
func (h *apiV1GreetHandler) handleGreetQuery(w http.ResponseWriter, r *http.Request) {
|
||||
name := r.URL.Query().Get("name")
|
||||
h.writeJSONResponse(w, h.greeter.Greet(name))
|
||||
h.writeJSONResponse(w, h.greeter.Greet(r.Context(), name))
|
||||
}
|
||||
|
||||
func (h *apiV1GreetHandler) handleGreetPath(w http.ResponseWriter, r *http.Request) {
|
||||
name := chi.URLParam(r, "name")
|
||||
h.writeJSONResponse(w, h.greeter.Greet(name))
|
||||
h.writeJSONResponse(w, h.greeter.Greet(r.Context(), name))
|
||||
}
|
||||
|
||||
func (h *apiV1GreetHandler) writeJSONResponse(w http.ResponseWriter, message string) {
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
package greet
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type Service struct{}
|
||||
|
||||
func NewService() *Service {
|
||||
@@ -9,7 +14,9 @@ func NewService() *Service {
|
||||
// Greet returns a greeting message for the given name.
|
||||
// If name is empty, it defaults to "world".
|
||||
// Implements the Greeter interface.
|
||||
func (s *Service) Greet(name string) string {
|
||||
func (s *Service) Greet(ctx context.Context, name string) string {
|
||||
log.Trace().Ctx(ctx).Str("name", name).Msg("Greet function called")
|
||||
|
||||
if name == "" {
|
||||
return "Hello world!"
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package greet
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestService_Greet(t *testing.T) {
|
||||
service := NewService()
|
||||
@@ -16,7 +19,7 @@ func TestService_Greet(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := service.Greet(tt.name)
|
||||
result := service.Greet(context.Background(), tt.name)
|
||||
if result != tt.expected {
|
||||
t.Errorf("Greet(%q) = %q, want %q", tt.name, result, tt.expected)
|
||||
}
|
||||
|
||||
@@ -2,11 +2,15 @@ package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"DanceLessonsCoach/pkg/greet"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
@@ -14,6 +18,11 @@ type Server struct {
|
||||
}
|
||||
|
||||
func NewServer() *Server {
|
||||
// Initialize Zerolog with Trace level
|
||||
zerolog.SetGlobalLevel(zerolog.TraceLevel)
|
||||
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
|
||||
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339})
|
||||
|
||||
s := &Server{
|
||||
router: chi.NewRouter(),
|
||||
}
|
||||
@@ -22,23 +31,27 @@ func NewServer() *Server {
|
||||
}
|
||||
|
||||
func (s *Server) setupRoutes() {
|
||||
s.router.Use(middleware.Logger)
|
||||
s.router.Route("/api", func(r chi.Router) {
|
||||
// Use Zerolog middleware instead of Chi's default logger
|
||||
s.router.Use(middleware.RequestLogger(&middleware.DefaultLogFormatter{
|
||||
Logger: &log.Logger,
|
||||
NoColor: false,
|
||||
}))
|
||||
|
||||
// Health endpoint at root level
|
||||
s.router.Get("/api/health", s.handleHealth)
|
||||
|
||||
// API routes
|
||||
s.router.Route("/api/v1", func(r chi.Router) {
|
||||
r.Use(s.apiMiddlewares()...)
|
||||
r.Get("/health", s.handleHealth)
|
||||
s.registerApiV1Routes(r)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) registerApiV1Routes(r chi.Router) {
|
||||
r.Route("/v1", func(r chi.Router) {
|
||||
|
||||
greetService := greet.NewService()
|
||||
greetHandler := greet.NewApiV1GreetHandler(greetService)
|
||||
r.Route("/greet", func(r chi.Router) {
|
||||
greetHandler.RegisterRoutes(r)
|
||||
})
|
||||
|
||||
greetService := greet.NewService()
|
||||
greetHandler := greet.NewApiV1GreetHandler(greetService)
|
||||
r.Route("/greet", func(r chi.Router) {
|
||||
greetHandler.RegisterRoutes(r)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -50,6 +63,7 @@ func (s *Server) apiMiddlewares() []func(http.Handler) http.Handler {
|
||||
}
|
||||
|
||||
func (s *Server) handleHealth(w http.ResponseWriter, r *http.Request) {
|
||||
log.Info().Msg("Health check requested")
|
||||
w.Write([]byte(`{"status":"healthy"}`))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user