# bekrobot99/42-ft_transcendence architecture

> Architecture diagram of the GitHub repository bekrobot99/42-ft_transcendence, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-11-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bekrobot99/42-ft_transcendence
- Repository: https://github.com/bekrobot99/42-ft_transcendence

## Overview

##### Project type and purpose
- This is a full-stack, real-time multiplayer ping-pong web application (2D & 3D modes).
- Frontend: TypeScript SPA (Canvas API + Babylon.js)
- Backend: Node.js & Fastify REST + WebSocket real-time server
- Data store: SQLite
- Containerized via Docker, reverse-proxied with nginx, SSL/TLS enabled

##### File structure
- Top-level “frontend/” and “backend/” folders indicate clear separation of presentation vs. application layers
- “backend/src/api/” contains REST endpoints (auth, profile, tournaments, etc.)
- “backend/src/websocket.ts” and related services implement real-time gameplay
- “frontend/src/views/” hosts visual pages (Home, Game2D, Game3D, etc.) and UI components under “ui/”
- Deployment artifacts: Dockerfiles for frontend, backend, Docker Compose files, nginx configs, SSL certificates

##### Key architecture points from README
- SPA served by nginx: static assets delivered over HTTPS
- Backend API & WebSocket endpoints handled by Fastify server
- JWT + Google OAuth + 2FA for secure authentication
- Tournament subsystem, user profiles, friend management all behind REST API
- Real-time game loops & state syncing via WebSocket channels

a. Client (Browser)
– Renders SPA, initiates HTTP(s) and WebSocket connections
b. nginx reverse proxy
– Terminates SSL, serves static assets, forwards API/WebSocket traffic
c. Frontend container
– Hosts index.html, compiled JS (Canvas & Babylon.js engines)
d. Backend container
– Fastify server exposes:
- REST API (authentication, user, tournament, profile, etc.)
- WebSocket endpoint for live game sessions
e. SQLite database
– Embedded file-based DB for persistent user, tournament, match records
f. External services
– Google OAuth endpoints for third-party login

##### Relationships & data flows
1. Browser → HTTPS → nginx → static assets OR proxy to Fastify REST
2. Browser WebSocket handshake → nginx → Fastify websocket.ts
3. Fastify → read/write SQLite via database config module
4. Fastify → external Google OAuth service for sign-in/up
– Request/response (HTTP REST)
– Bi-directional WebSocket streams
– Backend–DB interactions
– Backend–OAuth flows

##### Architectural patterns & principles
- Three-tier separation: Presentation (SPA), Application (Fastify), Data (SQLite)
- Containerized micro-deployment: individual Docker containers orchestrated by Docker Compose
- Secure boundary: nginx as reverse proxy + SSL + JWT gateways
- Real-time event-driven domain: WebSocket pub/sub for game state sync

##### Technologies & frameworks to annotate
- Frontend: TypeScript, Canvas API, Babylon.js, WebSocket client
- Backend: Node.js, Fastify, TypeScript, WebSocket server, bcrypt/TOTP for 2FA
- Database: SQLite via TypeORM or custom DB config
- DevOps: Docker, Docker Compose, nginx, railway/render configs

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client (Browser)" 
        direction TB
        BrowserSPA["SPA (Canvas API + Babylon.js)"]:::frontend
        BrowserWS["WebSocket Client"]:::frontend
    end

    %% Infrastructure
    nginx["nginx (SSL)"]:::infra
    subgraph "Certificates"
        direction TB
        certCrt["nginx.crt"]:::infra
        certKey["nginx.key"]:::infra
    end
    click nginx "https://github.com/bekrobot99/42-ft_transcendence/blob/main/nginx.conf"
    click nginx "https://github.com/bekrobot99/42-ft_transcendence/blob/main/nginx-render.conf"
    click certCrt "https://github.com/bekrobot99/42-ft_transcendence/blob/main/certificates/nginx.crt"
    click certKey "https://github.com/bekrobot99/42-ft_transcendence/blob/main/certificates/nginx.key"

    %% Frontend Container
    subgraph "Frontend Container" 
        direction TB
        indexHTML["index.html"]:::frontend
        assets["assets"]:::frontend
        appTS["app.ts"]:::frontend
        subgraph "Views"
            direction TB
            HomePage["HomePage.ts"]:::frontend
            GamePage["GamePage.ts"]:::frontend
            GamePage3D["GamePage3D.ts"]:::frontend
            ProfilePage["ProfilePage.ts"]:::frontend
            SettingsPage["SettingsPage.ts"]:::frontend
            SocialView["SocialView.ts"]:::frontend
            TournamentPage["TournamentPage.ts"]:::frontend
        end
        subgraph "UI Components"
            direction TB
            ConnectForm["ConnectForm.ts"]:::frontend
            LanguageDropdown["LanguageDropdown.ts"]:::frontend
            NavigationBar["NavigationBar.ts"]:::frontend
            SignUpForm["SingUpForm.ts"]:::frontend
        end
    end
    click indexHTML "https://github.com/bekrobot99/42-ft_transcendence/blob/main/frontend/index.html"
    click assets "https://github.com/bekrobot99/42-ft_transcendence/tree/main/frontend/assets"
    click appTS "https://github.com/bekrobot99/42-ft_transcendence/blob/main/frontend/src/app.ts"
    click HomePage "https://github.com/bekrobot99/42-ft_transcendence/blob/main/frontend/src/views/HomePage.ts"
    click GamePage "https://github.com/bekrobot99/42-ft_transcendence/blob/main/frontend/src/views/GamePage.ts"
    click GamePage3D "https://github.com/bekrobot99/42-ft_transcendence/blob/main/frontend/src/views/GamePage3D.ts"
    click ProfilePage "https://github.com/bekrobot99/42-ft_transcendence/blob/main/frontend/src/views/ProfilePage.ts"
    click SettingsPage "https://github.com/bekrobot99/42-ft_transcendence/blob/main/frontend/src/views/SettingsPage.ts"
    click SocialView "https://github.com/bekrobot99/42-ft_transcendence/blob/main/frontend/src/views/SocialView.ts"
    click TournamentPage "https://github.com/bekrobot99/42-ft_transcendence/blob/main/frontend/src/views/TournamentPage.ts"
    click ConnectForm "https://github.com/bekrobot99/42-ft_transcendence/blob/main/frontend/src/ui/ConnectForm.ts"
    click LanguageDropdown "https://github.com/bekrobot99/42-ft_transcendence/blob/main/frontend/src/ui/LanguageDropdown.ts"
    click NavigationBar "https://github.com/bekrobot99/42-ft_transcendence/blob/main/frontend/src/ui/NavigationBar.ts"
    click SignUpForm "https://github.com/bekrobot99/42-ft_transcendence/blob/main/frontend/src/ui/SingUpForm.ts"

    %% Backend Container
    subgraph "Backend Container"
        direction TB
        serverTS["server.ts"]:::backend
        pkgJSON["package.json"]:::backend
        envExample[".env.example"]:::backend

        subgraph "API Layer"
            direction TB
            authRoutes["auth.ts"]:::backend
            registerRoutes["registerRoutes.ts"]:::backend
            signinRoutes["signinRoutes.ts"]:::backend
            signoutRoutes["signoutRoutes.ts"]:::backend
            googleAuth["googleAuthRoutes.ts"]:::backend
            twofaRoutes["twofaRoutes.ts"]:::backend
            avatarRoutes["avatarRoutes.ts"]:::backend
            userRoutes["userRoutes.ts"]:::backend
            profileRoutes["profile.ts"]:::backend
            friendsRoutes["friends.ts"]:::backend
            tournamentRoutes["tournament.ts"]:::backend
        end

        websocket["websocket.ts"]:::backend

        subgraph "Services"
            direction TB
            passService["pass_service.ts"]:::backend
            twofaService["2fact.ts"]:::backend
            validators["validators.ts"]:::backend
        end

        subgraph "Database Config"
            direction TB
            dbConfig["database.ts"]:::backend
            sqliteFile["transcendence.sqlite"]:::db
        end
    end
    click serverTS "https://github.com/bekrobot99/42-ft_transcendence/blob/main/backend/src/server.ts"
    click pkgJSON "https://github.com/bekrobot99/42-ft_transcendence/blob/main/backend/package.json"
    click envExample "https://github.com/bekrobot99/42-ft_transcendence/blob/main/backend/.env.example"
    click authRoutes "https://github.com/bekrobot99/42-ft_transcendence/blob/main/backend/src/api/auth.ts"
    click registerRoutes "https://github.com/bekrobot99/42-ft_transcendence/blob/main/backend/src/api/registerRoutes.ts"
    click signinRoutes "https://github.com/bekrobot99/42-ft_transcendence/blob/main/backend/src/api/signinRoutes.ts"
    click signoutRoutes "https://github.com/bekrobot99/42-ft_transcendence/blob/main/backend/src/api/signoutRoutes.ts"
    click googleAuth "https://github.com/bekrobot99/42-ft_transcendence/blob/main/backend/src/api/googleAuthRoutes.ts"
    click twofaRoutes "https://github.com/bekrobot99/42-ft_transcendence/blob/main/backend/src/api/twofaRoutes.ts"
    click avatarRoutes "https://github.com/bekrobot99/42-ft_transcendence/blob/main/backend/src/api/avatarRoutes.ts"
    click userRoutes "https://github.com/bekrobot99/42-ft_transcendence/blob/main/backend/src/api/userRoutes.ts"
    click profileRoutes "https://github.com/bekrobot99/42-ft_transcendence/blob/main/backend/src/api/profile.ts"
    click friendsRoutes "https://github.com/bekrobot99/42-ft_transcendence/blob/main/backend/src/api/friends.ts"
    click tournamentRoutes "https://github.com/bekrobot99/42-ft_transcendence/blob/main/backend/src/api/tournament.ts"
    click websocket "https://github.com/bekrobot99/42-ft_transcendence/blob/main/backend/src/websocket.ts"
    click passService "https://github.com/bekrobot99/42-ft_transcendence/blob/main/backend/src/services/pass_service.ts"
    click twofaService "https://github.com/bekrobot99/42-ft_transcendence/blob/main/backend/src/services/2fact.ts"
    click validators "https://github.com/bekrobot99/42-ft_transcendence/blob/main/backend/src/services/validators.ts"
    click dbConfig "https://github.com/bekrobot99/42-ft_transcendence/blob/main/backend/src/config/database.ts"
    click sqliteFile "https://github.com/bekrobot99/42-ft_transcendence/blob/main/backend/database/transcendence.sqlite"

    %% External
    googleOAuth["Google OAuth"]:::external

    %% Connections
    BrowserSPA -->|"HTTPS"| nginx
    BrowserSPA -->|"WS Handshake"| nginx
    BrowserWS ~~~|"WebSocket"| nginx
    nginx -->|"Serve Static"| indexHTML
    nginx -->|"Proxy REST"| serverTS
    nginx -->|"Proxy WS"| websocket
    serverTS -->|"CRUD"| dbConfig
    dbConfig -->|"FS"| sqliteFile
    serverTS -->|".dashed external"| googleOAuth

    %% Styles
    classDef frontend fill:#D0E8FF,stroke:#0366d6,color:#0366d6;
    classDef backend fill:#DFFFE0,stroke:#28a745,color:#28a745;
    classDef db fill:#FFE8A0,stroke:#d69e2e,color:#d69e2e;
    classDef infra fill:#E0E0E0,stroke:#6a737d,color:#6a737d;
    classDef external fill:#F5E1E1,stroke:#d73a49,color:#d73a49;
```
