Co-authored-by: Gabriel Radureau <arcodange@gmail.com> Co-committed-by: Gabriel Radureau <arcodange@gmail.com>
1.7 KiB
1.7 KiB
Code Examples
Snippets and patterns used across the dance-lessons-coach codebase. Extracted from the original AGENTS.md (Tâche 6 restructure).
Adding a New API Endpoint
// 1. Add to interface
func (h *apiV1GreetHandler) RegisterRoutes(router chi.Router) {
router.Get("/", h.handleGreetQuery)
router.Get("/{name}", h.handleGreetPath)
router.Post("/custom", h.handleCustomGreet) // New endpoint
}
// 2. Implement handler
func (h *apiV1GreetHandler) handleCustomGreet(w http.ResponseWriter, r *http.Request) {
// Parse request
// Call service
// Return JSON response
}
Logging with Zerolog
// Trace level logging
log.Trace().Ctx(ctx).Str("key", "value").Msg("message")
// Info level
log.Info().Msg("Important event")
// Error level
log.Error().Err(err).Msg("Error occurred")
For the full logging strategy (when to use Trace vs Info, performance considerations), see ADR-0003 — Zerolog Logging.
Using context.Context
// Pass context through calls
func handler(w http.ResponseWriter, r *http.Request) {
result := service.Greet(r.Context(), "John")
// ...
}
// Create context with values
ctx := context.WithValue(r.Context(), "key", "value")
// Create context with timeout
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()
For the rationale behind context-aware services, see ADR-0004 — Interface-Based Design.
Best Practices Reminders
For higher-level guidance on code organization, error handling, performance, and testing, see AGENT_USAGE_GUIDE.md section "Best Practices".