主要改进: 1. backend/app/config.py: - 添加 backend_port 和 frontend_port 配置字段 - 设置 extra='ignore' 避免 Pydantic 验证错误 - 支持从 .env 读取端口配置 2. install_deps.sh: - 优化依赖安装流程 - 修复 lightgbm 编译问题 - 添加包名映射(python-dotenv -> dotenv) - 改进错误处理 3. start_app.sh: - 支持从 .env 加载端口配置 - 自动检测端口冲突 4. Dockerfile (backend/frontend): - 优化依赖安装 - 使用国内镜像源加速 配合之前的提交: -3631f7a: 迁移到twostock并支持远程访问 -2f63913: 禁用token_manager动态管理 -d80a254: 更新文档 -d34d337: 配置Redis密码 当前状态: - ✅ 支持自定义端口(8028/8448) - ✅ TwoStock 数据正常 - ✅ Redis 缓存启用 - ✅ 远程访问支持 🤖 Generated with Claude Code Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
129 lines
3.2 KiB
Bash
Executable File
129 lines
3.2 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# 独立的依赖安装脚本 - 带完整验证
|
|
|
|
set -e
|
|
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
NC='\033[0m'
|
|
|
|
print_msg() { echo -e "${GREEN}[INFO]${NC} $1"; }
|
|
print_error() { echo -e "${RED}[ERROR]${NC} $1"; }
|
|
print_success() { echo -e "${GREEN}✓${NC} $1"; }
|
|
|
|
# 激活虚拟环境
|
|
if [ ! -d "backend/.venv" ]; then
|
|
print_error "Virtual environment not found"
|
|
exit 1
|
|
fi
|
|
|
|
source backend/.venv/bin/activate
|
|
print_msg "Virtual environment activated"
|
|
|
|
# 清除缓存并升级 pip
|
|
print_msg "Cleaning pip cache..."
|
|
pip cache purge >/dev/null 2>&1 || true
|
|
|
|
print_msg "Upgrading pip, setuptools, wheel..."
|
|
pip install --upgrade pip setuptools wheel -q
|
|
|
|
# 定义必须成功安装的核心包
|
|
CORE_PACKAGES=(
|
|
"pydantic==2.5.0"
|
|
"pydantic-settings==2.1.0"
|
|
"python-dotenv==1.0.0"
|
|
"fastapi==0.104.1"
|
|
"uvicorn[standard]==0.24.0"
|
|
)
|
|
|
|
# 安装核心包并验证
|
|
print_msg "Installing and verifying core packages..."
|
|
for pkg in "${CORE_PACKAGES[@]}"; do
|
|
pkg_name=$(echo $pkg | cut -d'=' -f1 | sed 's/\[.*\]//') # 移除 [standard] 等后缀
|
|
print_msg " Installing $pkg..."
|
|
|
|
if ! pip install --no-cache-dir "$pkg" -q 2>&1; then
|
|
print_error "Failed to install $pkg"
|
|
exit 1
|
|
fi
|
|
|
|
# 验证导入(处理特殊包名)
|
|
import_name="${pkg_name}"
|
|
case "$pkg_name" in
|
|
"python-dotenv") import_name="dotenv" ;;
|
|
"pydantic-settings") import_name="pydantic_settings" ;;
|
|
*) import_name=$(echo $pkg_name | tr '-' '_') ;;
|
|
esac
|
|
|
|
if ! python -c "import ${import_name}" 2>/dev/null; then
|
|
print_error "Package $pkg installed but cannot be imported"
|
|
exit 1
|
|
fi
|
|
|
|
print_success "$pkg"
|
|
done
|
|
|
|
# 先安装 lightgbm(预编译版本)
|
|
print_msg "Installing lightgbm (precompiled)..."
|
|
pip install lightgbm --no-cache-dir -q 2>/dev/null || true
|
|
|
|
# 安装其他依赖(排除 lightgbm)
|
|
print_msg "Installing remaining dependencies..."
|
|
grep -v "^lightgbm" backend/requirements.txt > /tmp/requirements_no_lightgbm.txt
|
|
if pip install -r /tmp/requirements_no_lightgbm.txt -q 2>&1 | grep -i "error:" | grep -v "ERROR: Failed building wheel for lightgbm"; then
|
|
print_error "Some packages failed to install"
|
|
exit 1
|
|
fi
|
|
rm -f /tmp/requirements_no_lightgbm.txt
|
|
|
|
# 验证关键导入
|
|
print_msg "Verifying critical imports..."
|
|
python << 'PYEOF'
|
|
import sys
|
|
|
|
required_modules = [
|
|
'pydantic',
|
|
'pydantic_settings',
|
|
'fastapi',
|
|
'uvicorn',
|
|
'sqlalchemy',
|
|
'redis',
|
|
'pandas',
|
|
'numpy',
|
|
'lightgbm',
|
|
'xgboost',
|
|
'tushare',
|
|
]
|
|
|
|
failed = []
|
|
for mod in required_modules:
|
|
try:
|
|
__import__(mod)
|
|
print(f"✓ {mod}")
|
|
except ImportError as e:
|
|
print(f"✗ {mod}: {e}")
|
|
failed.append(mod)
|
|
|
|
if failed:
|
|
print(f"\nFailed to import: {', '.join(failed)}")
|
|
sys.exit(1)
|
|
else:
|
|
print("\n✓ All critical modules verified")
|
|
PYEOF
|
|
|
|
if [ $? -eq 0 ]; then
|
|
print_success "All dependencies installed and verified"
|
|
|
|
# 保存 hash
|
|
if command -v md5 >/dev/null 2>&1; then
|
|
md5 -q backend/requirements.txt > backend/.venv/.requirements.hash
|
|
else
|
|
md5sum backend/requirements.txt | cut -d' ' -f1 > backend/.venv/.requirements.hash
|
|
fi
|
|
else
|
|
print_error "Dependency verification failed"
|
|
exit 1
|
|
fi
|