從 Docker/macOS+Conda 部署遷移到 WSL2 Ubuntu 原生開發環境 主要變更: - 移除所有 Docker 相關配置檔案 (Dockerfile, docker-compose.yml, .dockerignore 等) - 移除 macOS/Conda 設置腳本 (SETUP.md, setup_conda.sh) - 新增 WSL Ubuntu 自動化環境設置腳本 (setup_dev_env.sh) - 新增後端/前端快速啟動腳本 (start_backend.sh, start_frontend.sh) - 統一開發端口配置 (backend: 8000, frontend: 5173) - 改進資料庫連接穩定性(連接池、超時設置、重試機制) - 更新專案文檔以反映當前 WSL 開發環境 Technical improvements: - Database connection pooling with health checks and auto-reconnection - Retry logic for long-running OCR tasks to prevent DB timeouts - Extended JWT token expiration to 24 hours - Support for Office documents (pptx, docx) via LibreOffice headless - Comprehensive system dependency installation in single script Environment: - OS: WSL2 Ubuntu 24.04 - Python: 3.12 (venv) - Node.js: 24.x LTS (nvm) - Backend Port: 8000 - Frontend Port: 5173 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
49 lines
1.2 KiB
Python
49 lines
1.2 KiB
Python
"""
|
|
Tool_OCR - Database Connection Management
|
|
SQLAlchemy setup with async support
|
|
"""
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.orm import sessionmaker
|
|
from app.core.config import settings
|
|
|
|
# Create database engine
|
|
engine = create_engine(
|
|
settings.database_url,
|
|
pool_pre_ping=True, # Enable connection health checks
|
|
pool_size=10,
|
|
max_overflow=20,
|
|
pool_recycle=3600, # Recycle connections every hour
|
|
pool_timeout=30, # Connection timeout
|
|
connect_args={
|
|
'connect_timeout': 10,
|
|
'read_timeout': 30,
|
|
'write_timeout': 30,
|
|
},
|
|
echo=False, # Set to True for SQL query logging
|
|
)
|
|
|
|
# Create session factory
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
# Base class for all models
|
|
Base = declarative_base()
|
|
|
|
|
|
# Dependency to get database session
|
|
def get_db():
|
|
"""
|
|
Database session dependency for FastAPI endpoints
|
|
|
|
Usage:
|
|
@app.get("/endpoint")
|
|
def endpoint(db: Session = Depends(get_db)):
|
|
# Use db session here
|
|
"""
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|