Architecture Documentation
Clean Architecture
This project follows Clean Architecture principles with clear separation of concerns.
Layers
- Domain Layer (
domain/)- Contains business entities and core business logic
- No dependencies on external frameworks
- Defines repository interfaces
- Entities embed
*ddd.BaseEntityfrom pericarp for event sourcing
- Application Layer (
application/)- Uber Fx dependency injection module
- Service provider functions
- Event handler subscriptions
- Use case orchestration
- Infrastructure Layer (
infrastructure/)- Database implementations (GORM with auto-detect SQLite/PostgreSQL)
- Event dispatcher provider (pericarp)
- Structured logging (Zap)
- External service clients
- Implements domain interfaces
- API Layer (
api/)- HTTP handlers (Echo v4)
- SPA middleware with embedded frontend assets
- Auth middleware
- Request validation
Dependency Flow
API → Application → Domain ← Infrastructure
All dependencies point inward toward the domain layer.
Design Patterns
- Repository Pattern: Abstract data access with interfaces in domain
- Dependency Injection: Uber Fx with Module pattern
- Event Sourcing: Domain events via pericarp BaseEntity
- Unit of Work: Atomic event persistence and dispatch
- CQRS: Command/event separation via pericarp dispatchers
- Service Layer: Business logic in domain services
- Middleware Pattern: Cross-cutting concerns (auth, logging, tracing, SPA)
Event Sourcing Flow
1. Entity.RecordEvent(payload, eventType)
2. Service tracks entity in UnitOfWork
3. UnitOfWork.Commit(ctx)
→ EventStore.Append(events)
→ EventDispatcher.Dispatch(events)
4. Event handlers update projections / trigger side-effects
Observability
- OpenTelemetry for distributed tracing
- Structured logging with context propagation via Zap
- Metrics collection via Prometheus
- Request ID correlation across services