## MOC: Backend-паттерны и архитектурные решения ### Слоистая архитектура ```mermaid graph TB HTTP[HTTP Request] --> Chi[Chi Router] Chi --> MW[Middleware: Auth, CORS, Rate Limit, Logger] MW --> H[Handler] H --> S[Service: бизнес-логика] S --> R[Repository: PostgreSQL/pgx] S --> C[Cache: Redis] S --> FS[File Storage: MinIO/S3] R --> PG[(PostgreSQL + PostGIS)] ``` ### Ключевые решения | Паттерн | Где реализовано | Заметка | |---------|----------------|---------| | Sentinel errors | `services/*.go` | [[atomic-sentinel-errors-go]] | | Dependency Injection (интерфейсы) | `services/*.go` | [[atomic-dependency-inversion-go]] | | Graceful shutdown | `cmd/api/main.go` | [[atomic-graceful-shutdown]] | | Refresh token rotation | `services/auth.go` | [[decision-jwt-refresh-storage]] | | Rate limiting (Redis) | `middleware/ratelimit_redis.go` | [[decision-rate-limiter-redis]] | | WebSocket origin check | `handlers/websocket.go` | [[atomic-websocket-origin-check]] | ### Error handling chain ```mermaid sequenceDiagram Handler->>Service: call Service->>Repo: query Repo-->>Service: pgx error Service-->>Handler: sentinel error (%w) Handler->>Handler: errors.Is() -> HTTP status ``` ### Тестирование | Уровень | Инструмент | Пример | |---------|-----------|--------| | Unit (сервисы) | `testing` + моки | [[atomic-test-patterns-go]] | | Integration | `testcontainers-go` | [[decision-test-strategy]] | | E2E | Playwright | [[decision-test-strategy]] | ### Связанные заметки - [[architecture-overview]] — общая архитектура - [[backend-auth-security]] — JWT и refresh token - [[backend-validation]] — валидация через go-playground/validator - [[backend-rate-limiting]] — rate limiting - [[deploy-production-readiness]] — production readiness #architecture #backend #golang #MOC #best-practice