# ahmed5145/chessmate architecture

> Architecture diagram of the GitHub repository ahmed5145/chessmate, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ahmed5145/chessmate
- Repository: https://github.com/ahmed5145/chessmate

## Overview

#### Purpose and Entry Points
ChessMate is a chess-coaching platform: users import Chess.com/Lichess games, spend credits to run Stockfish analysis, and receive personalized feedback. The backend is a Django application entered through `chess_mate/manage.py`, served via `chess_mate/chess_mate/asgi.py` or `chess_mate/chess_mate/wsgi.py`. The React client starts at `chess_mate/frontend/src/index.js` and composes the application in `chess_mate/frontend/src/App.js`.

#### Backend HTTP and Application Boundary
Django project routing is defined in `chess_mate/chess_mate/urls.py`; the core application exposes its API routes through `chess_mate/core/urls.py` and request handlers in `chess_mate/core/views.py`. This is the main trust boundary for JWT-authenticated user actions, game import/selection, analysis requests, account flows, and payments. Environment-specific deployment configuration is separated between `chess_mate/chess_mate/settings.py` and `chess_mate/chess_mate/settings_prod.py`.

#### Domain, Persistence, and Account State
`chess_mate/core/models.py` is the central persistent-domain layer, covering users’ platform state such as games, analyses, and credits. Django migrations live under `chess_mate/core/migrations`. Views and service layers should treat models as the durable source of truth, while transient analysis status/results can be cached. Administrative access is connected through `chess_mate/core/admin.py`.

#### Analysis Pipeline
The core business pipeline is split across `chess_mate/core/chess_services.py`, `chess_mate/core/game_analyzer.py`, and `chess_mate/core/tasks.py`. Requests initiate work; Celery tasks move expensive or long-running game analysis off the HTTP path. Analysis invokes the locally configured Stockfish engine, processes chess-game moves and positions, then persists or exposes results for later retrieval. Batch and single-game execution are represented in the frontend by `chess_mate/frontend/src/components/BatchAnalysis.js` and `chess_mate/frontend/src/components/SingleGameAnalysis.js`.

#### Feedback and External Chess Sources
`chess_mate/core/ai_feedback.py` produces personalized coaching output from analysis results, using an OpenAI API key configured via environment. Chess import and engine-facing orchestration belong to `chess_mate/core/chess_services.py`; external dependencies include Chess.com and Lichess APIs, Stockfish, and OpenAI. These integrations form failure-prone boundaries and should remain behind service/task interfaces rather than UI or model code.

#### Credits, Payments, and Async Infrastructure
Credit purchase and consumption are architecturally coupled to analysis authorization. Stripe payment integration is handled in `chess_mate/core/payment.py`, with frontend purchase/status screens in `chess_mate/frontend/src/components/Credits.js` and `chess_mate/frontend/src/components/PaymentSuccess.js`. Redis is required as the Celery broker/result or caching infrastructure; cache access is centralized in `chess_mate/core/cache_manager.py`. Rate limiting in `chess_mate/core/rate_limiter.py` protects externally costly endpoints.

#### Frontend Control Flow
The React SPA uses route composition in `chess_mate/frontend/src/routes/AppRoutes.js`, protected navigation in `chess_mate/frontend/src/routes/ProtectedRoute.js`, and shared authenticated-user state in `chess_mate/frontend/src/contexts/UserContext.js`. `chess_mate/frontend/src/api.js` is the client/server boundary for API calls and JWT handling. Primary user flows cover authentication, game fetching, analysis, feedback display, profiles, and payment completion through components under `chess_mate/frontend/src/components`.

#### Deployment and Operational Dependencies
The system requires Python/Django, Node/React, PostgreSQL or default SQLite, Redis, Celery workers, Stockfish, Stripe, OpenAI, email delivery, and external chess-platform APIs. AWS-related deployment/storage support is represented by `chess_mate/chess_mate/aws.py` and `chess_mate/chess_mate/storage_backends.py`. Database backup operations are isolated in `chess_mate/scripts/backup_db.py`.

Main entry points: [`chess_mate/core/views.py`](https://github.com/ahmed5145/chessmate/blob/main/chess_mate/core/views.py) (Core API), [`chess_mate/frontend/src/routes/AppRoutes.js`](https://github.com/ahmed5145/chessmate/blob/main/chess_mate/frontend/src/routes/AppRoutes.js) (Client routes), [`chess_mate/core/game_analyzer.py`](https://github.com/ahmed5145/chessmate/blob/main/chess_mate/core/game_analyzer.py) (Game analyzer), [`chess_mate/core/tasks.py`](https://github.com/ahmed5145/chessmate/blob/main/chess_mate/core/tasks.py) (Analysis tasks)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["React client"]
  node_react_entry["React entry<br/>SPA bootstrap<br/>[index.js]"]
  node_app_shell["App shell<br/>React composition<br/>[App.js]"]
  node_client_routes["Client routes<br/>routing & guards<br/>[AppRoutes.js]"]
  node_user_context["User state<br/>auth context<br/>[UserContext.js]"]
  node_api_client["API client<br/>JWT HTTP client<br/>[api.js]"]
  node_analysis_ui["Analysis UI<br/>batch & single-game flows<br/>[BatchAnalysis.js]"]
  node_credits_ui["Credits & payment UI<br/>purchase flows<br/>[Credits.js]"]
end

subgraph group_backend["Django application"]
  node_django_entry["Django entry<br/>application server<br/>[manage.py]"]
  node_project_routes["Project routing<br/>Django URL router<br/>[urls.py]"]
  node_core_api["Core API<br/>authenticated handlers<br/>[views.py]"]
  node_domain_models[("Domain models<br/>persistent state<br/>[models.py]")]
  node_rate_limiter["Rate limiter<br/>cost protection<br/>[rate_limiter.py]"]
  node_payment_service["Payment service<br/>Stripe integration<br/>[payment.py]"]
end

subgraph group_pipeline["Analysis pipeline"]
  node_chess_service["Chess service<br/>import orchestration<br/>[chess_services.py]"]
  node_analysis_tasks["Analysis tasks<br/>Celery jobs<br/>[tasks.py]"]
  node_game_analyzer["Game analyzer<br/>move analysis<br/>[game_analyzer.py]"]
  node_ai_feedback["Coaching feedback<br/>OpenAI feedback<br/>[ai_feedback.py]"]
end

subgraph group_external["Infrastructure & integrations"]
  node_redis[("Redis<br/>broker, results & cache")]
  node_external_services{{"External services"}}
end

node_react_entry -->|"mounts"| node_app_shell
node_app_shell -->|"composes"| node_client_routes
node_client_routes -->|"uses auth state"| node_user_context
node_client_routes -->|"serves flows"| node_analysis_ui
node_client_routes -->|"serves flows"| node_credits_ui
node_analysis_ui -->|"requests analysis"| node_api_client
node_credits_ui -->|"purchases credits"| node_api_client
node_api_client -->|"JWT API calls"| node_project_routes
node_django_entry -->|"loads"| node_project_routes
node_project_routes -->|"dispatches"| node_core_api
node_core_api -->|"protects costly endpoints"| node_rate_limiter
node_core_api -->|"reads and writes"| node_domain_models
node_core_api -->|"imports games"| node_chess_service
node_core_api -->|"queues analysis"| node_analysis_tasks
node_core_api -->|"handles payments"| node_payment_service
node_analysis_tasks -->|"broker and results"| node_redis
node_analysis_tasks -->|"executes"| node_game_analyzer
node_game_analyzer -->|"persists results"| node_domain_models
node_game_analyzer -->|"supplies analysis"| node_ai_feedback
node_ai_feedback -->|"stores feedback"| node_domain_models
node_chess_service -->|"imports games"| node_external_services
node_game_analyzer -->|"uses Stockfish"| node_external_services
node_ai_feedback -->|"uses OpenAI"| node_external_services
node_payment_service -->|"uses Stripe"| node_external_services

click node_react_entry "https://github.com/ahmed5145/chessmate/blob/main/chess_mate/frontend/src/index.js"
click node_app_shell "https://github.com/ahmed5145/chessmate/blob/main/chess_mate/frontend/src/App.js"
click node_client_routes "https://github.com/ahmed5145/chessmate/blob/main/chess_mate/frontend/src/routes/AppRoutes.js"
click node_user_context "https://github.com/ahmed5145/chessmate/blob/main/chess_mate/frontend/src/contexts/UserContext.js"
click node_api_client "https://github.com/ahmed5145/chessmate/blob/main/chess_mate/frontend/src/api.js"
click node_analysis_ui "https://github.com/ahmed5145/chessmate/blob/main/chess_mate/frontend/src/components/BatchAnalysis.js"
click node_credits_ui "https://github.com/ahmed5145/chessmate/blob/main/chess_mate/frontend/src/components/Credits.js"
click node_django_entry "https://github.com/ahmed5145/chessmate/blob/main/chess_mate/manage.py"
click node_project_routes "https://github.com/ahmed5145/chessmate/blob/main/chess_mate/chess_mate/urls.py"
click node_core_api "https://github.com/ahmed5145/chessmate/blob/main/chess_mate/core/views.py"
click node_domain_models "https://github.com/ahmed5145/chessmate/blob/main/chess_mate/core/models.py"
click node_rate_limiter "https://github.com/ahmed5145/chessmate/blob/main/chess_mate/core/rate_limiter.py"
click node_payment_service "https://github.com/ahmed5145/chessmate/blob/main/chess_mate/core/payment.py"
click node_chess_service "https://github.com/ahmed5145/chessmate/blob/main/chess_mate/core/chess_services.py"
click node_analysis_tasks "https://github.com/ahmed5145/chessmate/blob/main/chess_mate/core/tasks.py"
click node_game_analyzer "https://github.com/ahmed5145/chessmate/blob/main/chess_mate/core/game_analyzer.py"
click node_ai_feedback "https://github.com/ahmed5145/chessmate/blob/main/chess_mate/core/ai_feedback.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_react_entry,node_app_shell,node_client_routes,node_user_context,node_api_client,node_analysis_ui,node_credits_ui toneBlue
class node_django_entry,node_project_routes,node_core_api,node_domain_models,node_rate_limiter,node_payment_service toneAmber
class node_chess_service,node_analysis_tasks,node_game_analyzer,node_ai_feedback toneMint
class node_redis,node_external_services toneRose
```

## Components

### React client

- [React entry](https://github.com/ahmed5145/chessmate/blob/main/chess_mate/frontend/src/index.js): SPA bootstrap · `chess_mate/frontend/src/index.js`
- [App shell](https://github.com/ahmed5145/chessmate/blob/main/chess_mate/frontend/src/App.js): React composition · `chess_mate/frontend/src/App.js`
- [Client routes](https://github.com/ahmed5145/chessmate/blob/main/chess_mate/frontend/src/routes/AppRoutes.js): routing & guards · Protected routes and authenticated navigation. · `chess_mate/frontend/src/routes/AppRoutes.js`
- [User state](https://github.com/ahmed5145/chessmate/blob/main/chess_mate/frontend/src/contexts/UserContext.js): auth context · `chess_mate/frontend/src/contexts/UserContext.js`
- [API client](https://github.com/ahmed5145/chessmate/blob/main/chess_mate/frontend/src/api.js): JWT HTTP client · `chess_mate/frontend/src/api.js`
- [Analysis UI](https://github.com/ahmed5145/chessmate/blob/main/chess_mate/frontend/src/components/BatchAnalysis.js): batch & single-game flows · `chess_mate/frontend/src/components/BatchAnalysis.js`
- [Credits & payment UI](https://github.com/ahmed5145/chessmate/blob/main/chess_mate/frontend/src/components/Credits.js): purchase flows · `chess_mate/frontend/src/components/Credits.js`

### Django application

- [Django entry](https://github.com/ahmed5145/chessmate/blob/main/chess_mate/manage.py): application server · Served through ASGI or WSGI. · `chess_mate/manage.py`
- [Project routing](https://github.com/ahmed5145/chessmate/blob/main/chess_mate/chess_mate/urls.py): Django URL router · `chess_mate/chess_mate/urls.py`
- [Core API](https://github.com/ahmed5145/chessmate/blob/main/chess_mate/core/views.py): authenticated handlers · Trust boundary for accounts, games, analysis, and payments. · `chess_mate/core/views.py`
- [Domain models](https://github.com/ahmed5145/chessmate/blob/main/chess_mate/core/models.py): persistent state · Durable games, analyses, users, and credits. · `chess_mate/core/models.py`
- [Rate limiter](https://github.com/ahmed5145/chessmate/blob/main/chess_mate/core/rate_limiter.py): cost protection · `chess_mate/core/rate_limiter.py`
- [Payment service](https://github.com/ahmed5145/chessmate/blob/main/chess_mate/core/payment.py): Stripe integration · `chess_mate/core/payment.py`

### Analysis pipeline

- [Chess service](https://github.com/ahmed5145/chessmate/blob/main/chess_mate/core/chess_services.py): import orchestration · Encapsulates chess-source imports and engine-facing work. · `chess_mate/core/chess_services.py`
- [Analysis tasks](https://github.com/ahmed5145/chessmate/blob/main/chess_mate/core/tasks.py): Celery jobs · Moves long-running analysis off the HTTP path. · `chess_mate/core/tasks.py`
- [Game analyzer](https://github.com/ahmed5145/chessmate/blob/main/chess_mate/core/game_analyzer.py): move analysis · `chess_mate/core/game_analyzer.py`
- [Coaching feedback](https://github.com/ahmed5145/chessmate/blob/main/chess_mate/core/ai_feedback.py): OpenAI feedback · `chess_mate/core/ai_feedback.py`

### Infrastructure & integrations

- **Redis**: broker, results & cache
- **External services**: chess, engine, AI & payments · Chess.com, Lichess, Stockfish, OpenAI, and Stripe.

## Connections

- React entry → App shell: mounts
- App shell → Client routes: composes
- Client routes → User state: uses auth state
- Client routes → Analysis UI: serves flows
- Client routes → Credits & payment UI: serves flows
- Analysis UI → API client: requests analysis
- Credits & payment UI → API client: purchases credits
- API client → Project routing: JWT API calls
- Django entry → Project routing: loads
- Project routing → Core API: dispatches
- Core API → Rate limiter: protects costly endpoints
- Core API → Domain models: reads and writes
- Core API → Chess service: imports games
- Core API → Analysis tasks: queues analysis
- Core API → Payment service: handles payments
- Analysis tasks → Redis: broker and results
- Analysis tasks → Game analyzer: executes
- Game analyzer → Domain models: persists results
- Game analyzer → Coaching feedback: supplies analysis
- Coaching feedback → Domain models: stores feedback
- Chess service → External services: imports games
- Game analyzer → External services: uses Stockfish
- Coaching feedback → External services: uses OpenAI
- Payment service → External services: uses Stripe
