System Architecture
ClassQuiz is built as a modern, real-time quiz platform using a monorepo structure with a FastAPI backend and SvelteKit frontend.High-Level Architecture
Tech Stack
Backend
The backend is built with Python and several key frameworks:Core Framework
- FastAPI - Modern, high-performance web framework
- Located in:
classquiz/__init__.py - Provides REST API endpoints
- Auto-generated OpenAPI docs at
/api/docs
- Located in:
Database & ORM
- PostgreSQL - Primary database
- Ormar - Async ORM built on SQLAlchemy Core and Pydantic
- Models defined in:
classquiz/db/models.py - Supports async operations
- Type-safe with Pydantic validation
- Models defined in:
Real-time Communication
- python-socketio - WebSocket implementation
- Server implementation:
classquiz/socket_server/__init__.py - Enables real-time game interactions
- Handles player connections and game state
- Server implementation:
Caching & Sessions
- Redis - In-memory data store
- Game state management
- Session storage
- Player data caching
- Real-time leaderboards
Search
- Meilisearch - Fast, typo-tolerant search engine
- Quiz and content search
- Full-text search capabilities
Frontend
The frontend is a modern JavaScript application:Core Framework
- SvelteKit - Meta-framework for Svelte
- Server-side rendering (SSR)
- Static site generation
- API routes
- File-based routing
Styling
- TailwindCSS - Utility-first CSS framework
- @tailwindcss/typography - Typography plugin
Real-time Client
- socket.io-client (v4.8.1) - WebSocket client
- Connects to Socket.IO server
- Handles real-time game events
Key Libraries
- i18next - Internationalization
- @sentry/browser - Error tracking
- qrcode - QR code generation for game PINs
- luxon - Date/time handling
- canvas-confetti - Celebration effects
- felte - Form management
- dompurify - XSS protection
Project Structure
Core Components
API Routers
ClassQuiz organizes its REST API into modular routers:Configuration System
Settings are managed via Pydantic Settings (classquiz/config.py):
Middleware Stack
FastAPI middleware handles:- Session Management - Starlette SessionMiddleware
- Authentication - Custom remember-me middleware
- Error Tracking - Sentry exception capture
Data Flow
Quiz Gameplay Flow
Authentication Flow
- User logs in via REST API (
/api/v1/login) - JWT token generated with user claims
- Token stored in HTTP-only cookie or localStorage
- Middleware validates token on each request
- Session management via Redis for Socket.IO connections
Storage Architecture
ClassQuiz supports multiple storage backends:Local Storage
S3-Compatible Storage
classquiz/storage.py) handles file uploads, downloads, and management regardless of backend.
Security Features
Authentication Options
- Local - Email/password with optional 2FA (TOTP)
- WebAuthn/FIDO - Passwordless authentication
- OAuth2 - Google, GitHub, or custom OpenID providers
Data Protection
- Password hashing
- CSRF protection via session middleware
- XSS prevention (DOMPurify on frontend)
- Rate limiting capabilities
- Optional hCaptcha/reCaptcha for game joins
Deployment Architecture
Recommended Stack
Database Schema
The database uses PostgreSQL with these key tables:users- User accounts and authenticationquiz- Quiz definitions and questionsgame_results- Completed game statisticsstorage_items- Uploaded media filesuser_sessions- Active user sessionsapi_keys- API authentication tokens
Real-time Communication
Socket.IO handles all real-time features:- Player join/leave notifications
- Question synchronization
- Answer submission
- Live leaderboards
- Game state updates
- Admin controls
External Dependencies
Self-hostable
- PostgreSQL - Primary database
- Redis - Cache and session store
- Meilisearch - Search functionality
- Caddy - Reverse proxy (or nginx/Apache)
Optional Third-party Services
- Mapbox - Map-based questions
- hCaptcha - Bot protection
- Sentry - Error tracking
- Pixabay - Image search (requires API key)
Performance Considerations
Caching Strategy
- Redis caches frequently accessed data
- Game state stored in Redis for fast access
- Session data cached with 2-hour expiry
- Static assets served with cache headers
Async Architecture
- FastAPI runs on ASGI (async) server
- Ormar provides async database queries
- Socket.IO uses async event handlers
- Non-blocking I/O throughout
Scalability
- Stateless backend (session data in Redis)
- Horizontal scaling possible with load balancer
- Redis can be clustered for high availability
- PostgreSQL read replicas for read-heavy workloads