Documentation
¶
Overview ¶
Package adapters-nethttp demonstrates the three-layer codec pipeline pattern where every boundary — HTTP request, database, HTTP response — is modelled as a codec contract.
Three boundary codecs:
Codec[CreateUserReq] — HTTP request contract (what the client sends) Codec[UserRecord] — database contract (the SQL model) Codec[User] — HTTP response contract (what the client receives)
Two pure domain functions connect them (Layer 2):
buildUserRecord(CreateUserReq) UserRecord — request → DB record buildUserResponse(UserRecord) User — DB record → response
The pipeline for POST /users:
Codec[Req] ─ decode ─▶ CreateUserReq ─▶ buildUserRecord ─▶ UserRecord
↓ (store IO via Codec[UserRecord])
Codec[Resp] ─ encode ─▶ User ◀─ buildUserResponse ◀─ UserRecord
The infrastructure layer (Layer 3) owns all IO: HTTP adapter, database reads and writes. The store uses Codec[UserRecord] to encode/decode rows — the database schema is defined exactly once in the codec, just like the HTTP contracts.
Pure domain functions have zero IO and can be unit-tested with plain Go structs and no setup. Swap the entire infrastructure layer (HTTP → gRPC → CLI) without touching the domain layer or the business logic.
Routes:
- POST /users — body validate, JSON + YAML request formats, response headers + cookies (codec-validated), content negotiation
- GET /users/{id} — UUID path param, BuildPath type-safe URL construction
- GET /users — query params (page, search)
- GET /profile — request-side cookie + header validation
A CountingObserver wired into every route collects per-request metrics and per-field validation errors without any metrics library dependency.
Run with: go run ./examples/adapters-nethttp