Files
Task_Reporter/openspec/changes/archive/2025-12-07-add-unified-env-config/proposal.md
egg 92834dbe0e feat: Migrate to MySQL and add unified environment configuration
## Database Migration (SQLite → MySQL)
- Add Alembic migration framework
- Add 'tr_' prefix to all tables to avoid conflicts in shared database
- Remove SQLite support, use MySQL exclusively
- Add pymysql driver dependency
- Change ad_token column to Text type for long JWT tokens

## Unified Environment Configuration
- Centralize all hardcoded settings to environment variables
- Backend: Extend Settings class in app/core/config.py
- Frontend: Use Vite environment variables (import.meta.env)
- Docker: Move credentials to environment variables
- Update .env.example files with comprehensive documentation

## Test Organization
- Move root-level test files to tests/ directory:
  - test_chat_room.py → tests/test_chat_room.py
  - test_websocket.py → tests/test_websocket.py
  - test_realtime_implementation.py → tests/test_realtime_implementation.py
- Fix path references in test_realtime_implementation.py

Breaking Changes:
- CORS now requires explicit origins (no more wildcard)
- All database tables renamed with 'tr_' prefix
- SQLite no longer supported

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-07 14:15:11 +08:00

2.9 KiB
Raw Blame History

Change: Unified Environment Configuration Management

Why

目前專案中存在多處硬編碼的設定值端口、URL、超時時間、檔案大小限制等分散在後端 Python 程式碼、前端 TypeScript 程式碼、Docker 配置檔案及開發腳本中。這造成:

  1. 部署困難:每次部署到不同環境需要修改多個檔案
  2. 安全風險CORS 使用萬用字元 ["*"]、MinIO 使用預設密碼
  3. 維護成本:設定值分散在 20+ 個檔案中,難以追蹤和更新
  4. 環境一致性問題:開發、測試、生產環境難以保持設定同步

What Changes

Backend Configuration (Python)

  • 將所有硬編碼的設定值移至 app/core/config.py 的 Settings 類別
  • 擴展 .env 文件以包含所有可配置項目
  • 新增以下環境變數:
    • HOST, PORT, DEBUG - 伺服器設定
    • CORS_ORIGINS - CORS 來源白名單(BREAKING: 移除萬用字元)
    • SYSTEM_ADMIN_EMAIL - 系統管理員信箱
    • AD_API_TIMEOUT_SECONDS - AD API 超時設定
    • MESSAGE_EDIT_TIME_LIMIT_MINUTES - 訊息編輯時間限制
    • TYPING_TIMEOUT_SECONDS - 打字指示器超時
    • IMAGE_MAX_SIZE_MB, DOCUMENT_MAX_SIZE_MB, LOG_MAX_SIZE_MB - 檔案大小限制
    • LOG_LEVEL - 日誌等級

Frontend Configuration (TypeScript/Vite)

  • 使用 Vite 環境變數機制 (import.meta.env)
  • 新增以下環境變數:
    • VITE_API_TIMEOUT_MS - API 請求超時
    • VITE_MESSAGES_REFETCH_INTERVAL_MS - 訊息重新取得間隔
    • VITE_MAX_RECONNECT_DELAY_MS - WebSocket 重連延遲
    • VITE_REPORTS_STALE_TIME_MS - 報告快取過期時間
    • VITE_PORT - 開發伺服器端口
    • VITE_BACKEND_URL - 後端 API URL

Docker Configuration

  • docker-compose.minio.yml 中的硬編碼認證資訊改為環境變數
  • 新增 .env.docker 範例檔案

Documentation

  • 更新 .env.example 包含所有環境變數及說明
  • 更新 frontend/.env.example 包含所有前端環境變數

Impact

  • Affected specs: 新增 env-config spec
  • Affected code:
    • app/core/config.py - 擴展 Settings 類別
    • app/main.py - CORS 設定改為從環境變數讀取
    • app/modules/realtime/router.py - SYSTEM_ADMIN_EMAIL
    • app/modules/auth/services/ad_client.py - AD API 超時
    • app/modules/realtime/services/message_service.py - 訊息編輯限制
    • app/modules/realtime/websocket_manager.py - 打字超時
    • app/modules/file_storage/validators.py - 檔案大小限制
    • frontend/vite.config.ts - 端口和後端 URL
    • frontend/src/services/api.ts - API 超時
    • frontend/src/hooks/*.ts - 各種超時和間隔設定
    • docker-compose.minio.yml - MinIO 認證
    • .env.example, frontend/.env.example - 文件更新
  • Breaking changes:
    • CORS 設定從 ["*"] 改為必須明確指定來源
    • 新的必要環境變數可能導致現有部署需要更新 .env 檔案