feat(httpmw): initial stable release v0.9.0

Standalone net/http middleware for panic recovery, CORS, request ID injection, and request logging.

What's included:
- Recover(): panic -> 500, captures debug.Stack, no logger required
- CORS(origins): OPTIONS 204 preflight, origin allowlist, package-wide method/header constants
- RequestID(generator): injects ID via logz.WithRequestID, sets X-Request-ID response header
- RequestLogger(logger): logs method/path/status/latency/request_id; Error for 5xx, Info otherwise
- Logger interface: Info, Error, With — duck-typed; satisfied by logz.Logger
- StatusRecorder: exported http.ResponseWriter wrapper that captures written status code
- Direct logz import for context helpers (documented exception to ADR-001)

Tested-via: todo-api POC integration
Reviewed-against: docs/adr/
This commit is contained in:
2026-03-19 00:03:24 +00:00
commit ad2a9e465e
17 changed files with 739 additions and 0 deletions

22
requestid.go Normal file
View File

@@ -0,0 +1,22 @@
package httpmw
import (
"net/http"
"code.nochebuena.dev/go/logz"
)
// RequestID injects a unique request ID into the context (via logz.WithRequestID)
// and the X-Request-ID response header.
// generator is typically uuid.NewString.
func RequestID(generator func() string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
id := generator()
ctx := logz.WithRequestID(r.Context(), id)
r = r.WithContext(ctx)
w.Header().Set("X-Request-ID", id)
next.ServeHTTP(w, r)
})
}
}