5.3 KiB
Backend standard
The backend is a Go HTTP service. Keep transport and storage details out of domain rules.
Handwritten Go errors use Component.Method - operation: %w when wrapping a cause and Component.Method - reason when there is no cause. Free functions use package.Function as the location. Use real identifiers, English messages, and no trailing punctuation. Use errors.New for a static message without a cause; never pass nil to %w. Preserve errors.Is/As and stable sentinel identities, adding location at the call site. Pure pass-through returns need no extra wrapper. Keep external SDK errors sanitized before wrapping; do not expose credentials, private object keys, or URLs. Generated code is exempt. Log an error at its handling boundary rather than at every return.
Readability
- Extract repeated parsing, setup, cleanup, or policy into a small typed helper when it has one clear purpose. Keep helpers private and close to their consumers. Move code to pkg only when independent packages actually reuse it; do not create generic util packages or wrappers that only rename a call.
- Give non-obvious limits, timeouts, retry settings, protocol bounds, and shared fixture values descriptive constants. Keep them in their owning package. Derive counts from data with len instead of duplicating numbers. Equal values with different meanings need separate names. Obvious zero values, indexes, and clear test inputs can remain inline.
- Deployment-specific values belong in configuration, with named defaults and validation at the boundary. Parse each setting in one place and preserve the distinction between an unset value and an explicitly invalid value.
- Keep functions focused. Prefer guard clauses and early error returns; name complex conditions instead of building long boolean expressions. Choose names for the operation or rule rather than implementation details. Avoid needless abstractions, extra interfaces, and speculative generality.
- Keep dependencies and resource ownership explicit. Pass context first; propagate cancellation and deadlines. Register cleanup immediately after acquisition and release partial startup resources. Do not hide goroutines, global mutable state, or resource ownership inside convenience helpers.
- Check errors. Ignore a result only when it cannot change the outcome, and explain a non-obvious exception. Preserve machine-readable errors; do not compare error strings for control flow or expose internal errors through public HTTP responses.
- Comments explain constraints or decisions, not a restatement of the code. Use English. Prefer existing standard-library names and gofmt/goimports; keep handwritten code separate from generated output.
- Refactors preserve behavior and existing tests. Test changed boundaries and meaningful edge cases; do not add tests for constant names or helpers that merely mirror their implementation.
Keep domain types in one flat package at backend/internal/domain/. Add files such as user.go and candidate.go directly in that directory when their types are defined. Do not create domain subdirectories per entity or feature.
Use the existing separation between domain, usecase, port, adapter, and bootstrap code. Primary adapters receive external calls and translate them to application operations. Secondary adapters implement ports owned by their consumers. Bootstrap wires concrete adapters and controls process lifecycle.
Keep shared runtime configuration under backend/config/. Do not hide deployment settings in business packages. Do not add empty architectural packages ahead of a real use.
The generated ogen HTTP handler is mounted by the chi router. Keep handwritten HTTP concerns in the existing HTTP adapter and keep decisions that affect product rules in usecases or domain code. Health method names follow the healthz and readyz naming convention. The public API prefix is /api/v1; the standard metrics endpoint is internal and is not part of that prefix.
Use PostgreSQL through the repository's pgx and sqlc boundary. Schema changes are versioned migrations. Do not place SQL in HTTP handlers or domain packages.
Prefer the selected wahrwelt-kit packages only where they provide a useful technical boundary and have a pinned, verified version. Libraries such as logging, HTTP, metrics, and PostgreSQL helpers do not own product behavior. Do not add unused JWT, WebSocket, or other framework dependencies.
Redis belongs behind the outbound cache port and its secondary adapter. Bootstrap owns its client lifecycle. Redis, PostgreSQL, and the SeaweedFS bucket are required for readiness. Set cache entries with a positive TTL; future product consumers must tolerate misses, eviction, and restart. Do not treat cache entries as authoritative product records or add product caching without defining ownership and invalidation.
SeaweedFS belongs behind the outbound ObjectStorage port and the seaweedfs secondary adapter. Keep AWS SDK and S3 types inside that adapter. Bootstrap owns client resources; HeadBucket checks readiness without creating the bucket. Stream object content and close Get response bodies. Product upload/download authorization, content limits, and retention rules must be defined before exposing file operations.
Current code is an operational scaffold. Do not describe the health handler, database ping, or wiring as account, assessment, matching, or invitation behavior.