# son0ngu/chess_game architecture

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

- Interactive diagram: https://gitdiagram.com/son0ngu/chess_game
- Repository: https://github.com/son0ngu/chess_game

## Overview

##### Project type and purpose
- This is a full-stack, real-time chess game application.
- Frontend: React single-page app (UI, game board, user flows, socket & REST clients).
- Backend: Node.js/Express REST API + Socket.io server, with controllers, services, models, middleware, TLS certs, and a persistent database.

##### File structure
- Top-level split:
- chess_frontend/ – React app (public assets, components, pages, context, hooks, services, styles, utils)
- chess_backend/ – Express server (server.js, package.json, config, controllers, routes, services, models, middleware, socket handlers, utils, certs)
- Patterns in backend:
- MVC-style layering (routes → controllers → services → models)
- Middleware for auth, validation, error handling
- Socket namespace for real-time game events
- Presence of config/db.js implies database connectivity (e.g., MongoDB or SQL)
- TLS certs folder indicates HTTPS/WSS

##### README insights
- “fix, refactor and security now” suggests highlighting auth (JWT?), HTTPS, input validation layers

##### Detail and essential elements
– Break out backend into: Routes → Controllers → Services → Models → Database
– Show middleware chain sitting between “Client” and “Controllers/Socket handlers.”
– Include the logger utility hooked into both HTTP and WS flows.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend (React SPA)"
        direction TB
        ReactSPA["React SPA"]:::frontend

        subgraph "UI Components"
            direction TB
            Board["Board"]:::frontend
            BoardSquare["BoardSquare"]:::frontend
            GameControls["GameControls"]:::frontend
            MoveHistory["MoveHistory"]:::frontend
            Spinner["Spinner"]:::frontend
        end

        subgraph "Pages (Router)"
            direction TB
            Home["Home"]:::frontend
            Lobby["Lobby"]:::frontend
            Play["Play"]:::frontend
            SignIn["SignIn"]:::frontend
            SignUp["SignUp"]:::frontend
            PWRec["PWRec"]:::frontend
            ResetPW["ResetPW"]:::frontend
        end

        AuthContext["AuthContext"]:::frontend
        useChess["useChess"]:::frontend

        subgraph "Services"
            direction TB
            apiClient["api.js (REST client)"]:::frontend
            socketClient["socket.js (Socket client)"]:::frontend
        end

        storageUtil["storage.js"]:::frontend
        styles["CSS Styles"]:::frontend

        ReactSPA --> apiClient
        ReactSPA --> socketClient
        ReactSPA --> AuthContext
        ReactSPA --> useChess
    end

    %% Backend Layer
    subgraph "Backend (Node.js/Express)"
        direction TB

        certs["TLS Certificates"]:::infra
        dbConfig["db.js (DB Config)"]:::http
        logger["logger.js"]:::http
        gameAdapter["gameAdapter.js"]:::http

        subgraph "Middleware"
            direction TB
            authMiddleware["auth.js"]:::http
            userValidator["userValidator.js"]:::http
            errorHandler["errorHandler.js"]:::http
        end

        subgraph "HTTP Flow"
            direction TB
            authRoutes["auth.js"]:::http
            userRoutes["userRoutes.js"]:::http
            gameRoutes["game.js"]:::http

            userController["userController.js"]:::http
            gameController["gameController.js"]:::http

            GameService["GameService.js"]:::http

            userModel["user.js"]:::db
            gameModel["Game.js"]:::db

            authRoutes --> userController
            userRoutes --> userController
            gameRoutes --> gameController

            userController --> GameService
            gameController --> GameService

            GameService --> userModel
            GameService --> gameModel
        end

        subgraph "WebSocket Flow"
            direction TB
            gameSocket["gameSocket.js"]:::ws
            wsGameService["GameService.js"]:::ws

            gameSocket --> wsGameService
            wsGameService --> gameAdapter
            wsGameService --> logger
        end

        certs -->|terminates TLS| authRoutes
        certs -->|terminates TLS| gameSocket
        dbConfig --> userModel
        dbConfig --> gameModel
        logger --> authRoutes
        logger --> gameSocket
        authMiddleware --> authRoutes
        authMiddleware --> gameSocket
        userValidator --> authRoutes
        errorHandler --> authRoutes
    end

    %% Database
    Database[(Database\nusers, games)]:::db

    %% Infrastructure
    TLSCA["Certificate Authority"]:::infra

    %% Connections
    apiClient -->|REST/HTTPS| authRoutes
    apiClient -->|REST/HTTPS| userRoutes
    apiClient -->|REST/HTTPS| gameRoutes

    socketClient -->|WSS| gameSocket
    wsGameService -->|emit events| socketClient

    userModel --> Database
    gameModel --> Database

    TLSCA --> certs

    %% Click events
    click ReactSPA "https://github.com/son0ngu/chess_game/tree/main/chess_frontend/"
    click Board "https://github.com/son0ngu/chess_game/blob/main/chess_frontend/src/components/Board.jsx"
    click BoardSquare "https://github.com/son0ngu/chess_game/blob/main/chess_frontend/src/components/BoardSquare.jsx"
    click GameControls "https://github.com/son0ngu/chess_game/blob/main/chess_frontend/src/components/GameControls.jsx"
    click MoveHistory "https://github.com/son0ngu/chess_game/blob/main/chess_frontend/src/components/MoveHistory.jsx"
    click Spinner "https://github.com/son0ngu/chess_game/blob/main/chess_frontend/src/components/Spinner.jsx"
    click Home "https://github.com/son0ngu/chess_game/blob/main/chess_frontend/src/pages/Home.jsx"
    click Lobby "https://github.com/son0ngu/chess_game/blob/main/chess_frontend/src/pages/Lobby.jsx"
    click Play "https://github.com/son0ngu/chess_game/blob/main/chess_frontend/src/pages/Play.jsx"
    click SignIn "https://github.com/son0ngu/chess_game/blob/main/chess_frontend/src/pages/SignIn.jsx"
    click SignUp "https://github.com/son0ngu/chess_game/blob/main/chess_frontend/src/pages/SignUp.jsx"
    click PWRec "https://github.com/son0ngu/chess_game/blob/main/chess_frontend/src/pages/PWRec.jsx"
    click ResetPW "https://github.com/son0ngu/chess_game/blob/main/chess_frontend/src/pages/ResetPW.jsx"
    click AuthContext "https://github.com/son0ngu/chess_game/blob/main/chess_frontend/src/context/AuthContext.jsx"
    click useChess "https://github.com/son0ngu/chess_game/blob/main/chess_frontend/src/hooks/useChess.js"
    click apiClient "https://github.com/son0ngu/chess_game/blob/main/chess_frontend/src/services/api.js"
    click socketClient "https://github.com/son0ngu/chess_game/blob/main/chess_frontend/src/services/socket.js"
    click storageUtil "https://github.com/son0ngu/chess_game/blob/main/chess_frontend/src/utils/storage.js"
    click ReactSPA "https://github.com/son0ngu/chess_game/tree/main/chess_frontend/"
    click certs "https://github.com/son0ngu/chess_game/tree/main/chess_backend/certs/"
    click dbConfig "https://github.com/son0ngu/chess_game/blob/main/chess_backend/src/config/db.js"
    click authRoutes "https://github.com/son0ngu/chess_game/blob/main/chess_backend/src/routes/auth.js"
    click userRoutes "https://github.com/son0ngu/chess_game/blob/main/chess_backend/src/routes/userRoutes.js"
    click gameRoutes "https://github.com/son0ngu/chess_game/blob/main/chess_backend/src/routes/game.js"
    click userController "https://github.com/son0ngu/chess_game/blob/main/chess_backend/src/controllers/userController.js"
    click gameController "https://github.com/son0ngu/chess_game/blob/main/chess_backend/src/controllers/gameController.js"
    click GameService "https://github.com/son0ngu/chess_game/blob/main/chess_backend/src/services/GameService.js"
    click userModel "https://github.com/son0ngu/chess_game/blob/main/chess_backend/src/models/user.js"
    click gameModel "https://github.com/son0ngu/chess_game/blob/main/chess_backend/src/models/Game.js"
    click authMiddleware "https://github.com/son0ngu/chess_game/blob/main/chess_backend/src/middleware/auth.js"
    click userValidator "https://github.com/son0ngu/chess_game/blob/main/chess_backend/src/middleware/userValidator.js"
    click errorHandler "https://github.com/son0ngu/chess_game/blob/main/chess_backend/src/middleware/errorHandler.js"
    click gameSocket "https://github.com/son0ngu/chess_game/blob/main/chess_backend/src/socket/gameSocket.js"
    click gameAdapter "https://github.com/son0ngu/chess_game/blob/main/chess_backend/src/utils/gameAdapter.js"
    click logger "https://github.com/son0ngu/chess_game/blob/main/chess_backend/src/utils/logger.js"

    %% Styles
    classDef frontend fill:#D0E8FF,stroke:#0366d6,stroke-width:1px
    classDef http fill:#DFFFD6,stroke:#28a745,stroke-width:1px
    classDef ws fill:#FFE8D6,stroke:#ff8c00,stroke-width:1px
    classDef db fill:#F0F0F0,stroke:#6a737d,stroke-width:1px
    classDef infra fill:#EBD3FF,stroke:#6f42c1,stroke-width:1px
```
