📝 docs: cherry-pick 6 focused guides from PR #17 (CLI, CODE_EXAMPLES, HISTORY, OBSERVABILITY, ROADMAP, TROUBLESHOOTING)
This commit is contained in:
59
documentation/CODE_EXAMPLES.md
Normal file
59
documentation/CODE_EXAMPLES.md
Normal file
@@ -0,0 +1,59 @@
|
||||
# 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
|
||||
|
||||
```go
|
||||
// 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
|
||||
|
||||
```go
|
||||
// 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](../adr/0003-zerolog-logging.md).
|
||||
|
||||
## Using `context.Context`
|
||||
|
||||
```go
|
||||
// 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](../adr/0004-interface-based-design.md).
|
||||
|
||||
## Best Practices Reminders
|
||||
|
||||
For higher-level guidance on code organization, error handling, performance, and testing, see [`AGENT_USAGE_GUIDE.md`](AGENT_USAGE_GUIDE.md#best-practices) section "Best Practices".
|
||||
Reference in New Issue
Block a user