poker/ARCHITECTURE.md

20 KiB

🏗️ Архитектура развертывания

📊 Схема развертывания

┌─────────────────────────────────────────────────────────────────┐
│                         ИНТЕРНЕТ                                │
└────────────────────────────┬────────────────────────────────────┘
                             │
                             │ HTTP/HTTPS (80/443)
                             ▼
┌─────────────────────────────────────────────────────────────────┐
│                      APACHE 2.4                                 │
│  ┌───────────────────────────────────────────────────────────┐  │
│  │ • Reverse Proxy                                           │  │
│  │ • WebSocket Support (proxy_wstunnel)                      │  │
│  │ • SSL Termination                                         │  │
│  │ • Security Headers                                        │  │
│  └───────────────────────────────────────────────────────────┘  │
└────────────────────────────┬────────────────────────────────────┘
                             │
                             │ Proxy Pass (3336)
                             ▼
┌─────────────────────────────────────────────────────────────────┐
│                    DOCKER CONTAINER                             │
│  ┌───────────────────────────────────────────────────────────┐  │
│  │              Texas Hold'em Poker                          │  │
│  │                                                           │  │
│  │  ┌─────────────────────────────────────────────────────┐  │  │
│  │  │         Node.js 18 (Alpine Linux)                   │  │  │
│  │  │                                                     │  │  │
│  │  │  ┌────────────────┐    ┌────────────────┐          │  │  │
│  │  │  │  Express.js    │◄───┤   WebSocket    │          │  │  │
│  │  │  │   REST API     │    │   Server (ws)  │          │  │  │
│  │  │  └───────┬────────┘    └───────┬────────┘          │  │  │
│  │  │          │                     │                   │  │  │
│  │  │          └─────────┬───────────┘                   │  │  │
│  │  │                    ▼                               │  │  │
│  │  │          ┌──────────────────┐                      │  │  │
│  │  │          │   Database.js    │                      │  │  │
│  │  │          └─────────┬────────┘                      │  │  │
│  │  │                    │                               │  │  │
│  │  │                    ▼                               │  │  │
│  │  │          ┌──────────────────┐                      │  │  │
│  │  │          │  SQLite (sql.js) │                      │  │  │
│  │  │          └──────────────────┘                      │  │  │
│  │  │                                                     │  │  │
│  │  │  Port: 3336                                        │  │  │
│  │  └─────────────────────────────────────────────────────┘  │  │
│  └───────────────────────────────────────────────────────────┘  │
└────────────────────────────┬────────────────────────────────────┘
                             │
                             │ Volumes
                             ▼
┌─────────────────────────────────────────────────────────────────┐
│                      HOST FILESYSTEM                            │
│  ┌───────────────────────────────────────────────────────────┐  │
│  │  ./data/poker.db     ◄─── База данных SQLite             │  │
│  │  ./logs/             ◄─── Логи приложения                │  │
│  └───────────────────────────────────────────────────────────┘  │
└─────────────────────────────────────────────────────────────────┘

🔄 Поток данных

HTTP Request Flow

1. Клиент → Apache (80/443)
2. Apache → Docker (3336)
3. Express.js обрабатывает запрос
4. Database.js → SQLite
5. Ответ возвращается клиенту

WebSocket Flow

1. Клиент инициирует WS соединение
2. Apache proxy_wstunnel → Docker (3336)
3. WebSocket Server (ws) принимает подключение
4. Двусторонняя связь в реальном времени

📦 Компоненты

Frontend (public/)

┌──────────────────────────┐
│    Static Files          │
├──────────────────────────┤
│ • index.html             │
│ • main.js                │
│ • game.js                │
│ • ai.js                  │
│ • auth.js                │
│ • styles.css             │
└──────────────────────────┘

Backend (Node.js)

┌──────────────────────────┐
│    server.js             │
├──────────────────────────┤
│ • Express App            │
│ • HTTP Server            │
│ • WebSocket Server       │
│ • REST API Endpoints     │
│ • Middleware             │
└──────────────────────────┘

Database Layer

┌──────────────────────────┐
│    database.js           │
├──────────────────────────┤
│ • User Management        │
│ • Settings Storage       │
│ • Logs Storage           │
│ • JWT Tokens             │
│ • bcrypt Hashing         │
└──────────────────────────┘

🔐 Безопасность

┌─────────────────────────────────────────────────────────────┐
│                    Security Layers                          │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  1. Apache Layer                                            │
│     ├─ SSL/TLS (HTTPS)                                      │
│     ├─ Security Headers                                     │
│     └─ Rate Limiting (опционально)                          │
│                                                             │
│  2. Application Layer                                       │
│     ├─ JWT Authentication                                   │
│     ├─ bcrypt Password Hashing                              │
│     ├─ authMiddleware                                       │
│     ├─ adminMiddleware                                      │
│     └─ Input Validation                                     │
│                                                             │
│  3. Database Layer                                          │
│     ├─ Prepared Statements                                  │
│     ├─ User Ban System                                      │
│     └─ Action Logging                                       │
│                                                             │
└─────────────────────────────────────────────────────────────┘

🌐 Network Ports

╔══════════════════════════════════════════════════════════════╗
║                        PORT MAPPING                          ║
╠══════════════════════════════════════════════════════════════╣
║                                                              ║
║  External (Internet)                                         ║
║       │                                                      ║
║       ├── :80 (HTTP)  ──────┐                               ║
║       └── :443 (HTTPS) ──┐  │                               ║
║                          │  │                               ║
║  Apache                  │  │                               ║
║       ├── :80  <─────────┘  │                               ║
║       └── :443 <────────────┘                               ║
║           │                                                  ║
║           └── Proxy to localhost:3336                       ║
║                          │                                   ║
║  Docker                  │                                   ║
║       └── :3336 <────────┘                                  ║
║                                                              ║
╚══════════════════════════════════════════════════════════════╝

💾 Data Persistence

┌─────────────────────────────────────────────────────────────┐
│                    Data Flow                                │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  Application (Container)                                    │
│       │                                                     │
│       │ Write                                               │
│       ▼                                                     │
│  /app/data/poker.db  ◄─────────► Volume Mount              │
│       │                                  │                  │
│       │                                  │                  │
│       ▼                                  ▼                  │
│  Host: ./data/poker.db                                      │
│                                                             │
│  ✅ Сохраняется при перезапуске контейнера                  │
│  ✅ Доступно для backup                                     │
│  ✅ Можно редактировать напрямую                            │
│                                                             │
└─────────────────────────────────────────────────────────────┘

🔄 Deployment Flow

┌─────────────────────────────────────────────────────────────┐
│                    Deployment Process                       │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
                    ┌──────────────────┐
                    │  Clone Repo      │
                    └────────┬─────────┘
                             │
                             ▼
                    ┌──────────────────┐
                    │  ./deploy.sh     │
                    │  ./deploy.ps1    │
                    └────────┬─────────┘
                             │
                ┌────────────┼────────────┐
                │            │            │
                ▼            ▼            ▼
         ┌──────────┐ ┌──────────┐ ┌──────────┐
         │  Check   │ │  Create  │ │  Build   │
         │  Docker  │ │  Dirs    │ │  Image   │
         └────┬─────┘ └────┬─────┘ └────┬─────┘
              │            │            │
              └────────────┼────────────┘
                           │
                           ▼
                  ┌─────────────────┐
                  │  Start Container│
                  └────────┬────────┘
                           │
                           ▼
                  ┌─────────────────┐
                  │  Verify Status  │
                  └────────┬────────┘
                           │
                           ▼
                    ┌──────────────┐
                    │   ✅ Ready   │
                    └──────────────┘

🎯 Production Architecture

┌─────────────────────────────────────────────────────────────┐
│                    Production Setup                         │
└─────────────────────────────────────────────────────────────┘

         Internet
            │
            ▼
    ┌──────────────┐
    │   Firewall   │
    └──────┬───────┘
           │
           ▼
    ┌──────────────┐
    │   Apache     │ ◄── SSL Certificates (Let's Encrypt)
    │   (80/443)   │
    └──────┬───────┘
           │
           ▼
    ┌──────────────┐
    │   Docker     │
    │   (3336)     │ ◄── Auto-restart: unless-stopped
    └──────┬───────┘
           │
    ┌──────┴───────┬──────────┐
    │              │          │
    ▼              ▼          ▼
┌────────┐   ┌─────────┐  ┌──────┐
│ SQLite │   │  Logs   │  │Backup│
│ Volume │   │ Volume  │  │ Cron │
└────────┘   └─────────┘  └──────┘

📊 Monitoring Points

┌─────────────────────────────────────────────────────────────┐
│                    Monitoring Setup                         │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  1. Docker Logs                                             │
│     docker-compose logs -f                                  │
│                                                             │
│  2. Apache Logs                                             │
│     /var/log/apache2/poker_error.log                        │
│     /var/log/apache2/poker_access.log                       │
│                                                             │
│  3. Application Logs                                        │
│     ./logs/*.log (if configured)                            │
│                                                             │
│  4. Database Size                                           │
│     du -h data/poker.db                                     │
│                                                             │
│  5. Container Status                                        │
│     docker-compose ps                                       │
│     docker stats texas-holdem-poker                         │
│                                                             │
└─────────────────────────────────────────────────────────────┘

Дата: 2026-02-01
Версия: 1.0.0
Статус: Production Architecture Ready