Introduction
Consensus is a web-based e-voting platform built with TypeScript and Fastify. The architecture follows a layered design with clear separation of concerns, leveraging proven design patterns to ensure maintainability, testability, and scalability.Architecture Layers
The application follows a classic three-tier architecture:1. Presentation Layer
Technology: Fastify web framework with EJS templating Components:- Controllers: Handle HTTP requests/responses, orchestrate service calls
- Routes: Define URL endpoints and middleware
- Views: Server-side rendered EJS templates
src/web/server.ts:57-263- Main server setup and configurationsrc/controllers/- Controller implementationssrc/web/routes/- Route definitions
2. Business Layer
Components:- Services: Encapsulate business logic and orchestrate operations
- Domain Entities: Core business objects with encapsulated state
- Design Patterns: Strategy, Observer, Factory, Adapter patterns
VotingService- Manages vote casting and result calculationElectionService- Handles election lifecycle and state transitionsVoterService- Manages voter registration and authentication
3. Data Layer
Technology: SQLite with better-sqlite3 Components:- Repositories: Abstract data access using Repository pattern
- Database Connection: Singleton pattern for connection management
- Migrations: Version-controlled schema changes
Core Design Principles
Dependency Injection
All dependencies are explicitly injected through constructors, enabling testability and loose coupling:Encapsulation
Domain entities use private fields with getters/setters to control access and enforce invariants:Interface Segregation
Repositories implement focused interfaces defining clear contracts:Technology Stack
| Layer | Technology | Purpose |
|---|---|---|
| Runtime | Node.js | JavaScript runtime environment |
| Language | TypeScript | Type-safe development |
| Web Framework | Fastify | High-performance web server |
| Database | SQLite (better-sqlite3) | Embedded relational database |
| Templates | EJS | Server-side view rendering |
| Session Management | @fastify/session | User authentication |
| Password Hashing | bcrypt | Secure credential storage |
Security Considerations
Vote Anonymity
The system ensures vote anonymity by separating ballot storage from voter confirmations:- Ballots: Stored anonymously with no voter linkage
- Confirmations: Record that a voter voted, but not their choices
- Adapter Pattern:
AnonymousBallotAdapterenforces this separation
Authentication
Multiple authentication mechanisms:- Voter Authentication: Email + password with session management
- Admin Authentication: Username + password with role-based access
- Password Security: bcrypt hashing with salt
Data Integrity
- Database Constraints: Foreign keys and unique constraints
- Transaction Support: SQLite transactions for atomic operations
- Audit Logging: Observer pattern tracks all election state changes
Scalability Considerations
While SQLite is suitable for small to medium deployments: Current Scale:- Designed for organizations with hundreds to thousands of voters
- Single-server deployment model
- In-memory session storage
- Repository pattern enables easy migration to PostgreSQL/MySQL
- Service layer can be split into microservices
- Session storage can move to Redis
- Horizontal scaling with load balancers
Next Steps
- Domain Model - Detailed entity relationships
- Design Patterns - Pattern implementations
- Data Layer - Repository and database details