# 游铛铛客户端后端启动脚本 (Windows PowerShell) $ErrorActionPreference = "Stop" $GREEN = "Green" $RED = "Red" $YELLOW = "Yellow" $CYAN = "Cyan" Write-Host "========================================" -ForegroundColor $GREEN Write-Host " 游铛铛客户端后端启动脚本" -ForegroundColor $GREEN Write-Host "========================================" -ForegroundColor $GREEN # 检查 Python try { $pythonVersion = python --version 2>&1 Write-Host "[OK] Python已安装: $pythonVersion" -ForegroundColor $GREEN } catch { Write-Host "[错误] 未找到Python,请先安装Python3" -ForegroundColor $RED exit 1 } # 检查是否在 backend-client 目录 if (-not (Test-Path "app\main.py")) { Write-Host "[错误] 请在 backend-client 目录下运行此脚本" -ForegroundColor $RED exit 1 } # 解析启动模式 $Mode = "dev" if ($args[0] -eq "prod" -or $args[0] -eq "production") { $Mode = "prod" } Write-Host "" Write-Host "启动模式: $Mode" -ForegroundColor $CYAN # 检查依赖 if (-not (Test-Path "venv") -and -not (Test-Path ".venv")) { Write-Host "[警告] 未找到虚拟环境" -ForegroundColor $YELLOW $install = Read-Host "是否自动创建虚拟环境并安装依赖? (Y/N)" if ($install -eq "Y" -or $install -eq "y") { Write-Host "创建虚拟环境..." -ForegroundColor $CYAN python -m venv venv Write-Host "安装依赖..." -ForegroundColor $CYAN & .\venv\Scripts\pip install -r requirements.txt } } # 检查端口是否被占用 $port = 9000 $connection = Get-NetTCPConnection -LocalPort $port -ErrorAction SilentlyContinue if ($connection) { Write-Host "[警告] 端口 $port 已被占用!" -ForegroundColor $YELLOW Write-Host "占用进程:" $connection | Format-Table -AutoSize $kill = Read-Host "是否杀掉占用进程并继续? (Y/N)" if ($kill -eq "Y" -or $kill -eq "y") { $pids = $connection | Select-Object -ExpandProperty OwningProcess -Unique foreach ($pid in $pids) { Stop-Process -Id $pid -Force -ErrorAction SilentlyContinue Write-Host "已终止进程 PID: $pid" -ForegroundColor $YELLOW } Start-Sleep -Seconds 1 } else { Write-Host "启动取消" -ForegroundColor $RED exit 1 } } Write-Host "" Write-Host "API地址: http://125.122.32.114:9000" -ForegroundColor $CYAN Write-Host "API文档: http://125.122.32.114:9000/docs" -ForegroundColor $CYAN Write-Host "" Write-Host "按 Ctrl+C 停止服务" -ForegroundColor $YELLOW Write-Host "" # 启动服务 if ($Mode -eq "prod") { Write-Host "启动生产服务器 (4 workers)..." -ForegroundColor $GREEN python -m uvicorn app.main:app --host 0.0.0.0 --port $port --workers 4 } else { Write-Host "启动开发服务器 (reload模式)..." -ForegroundColor $GREEN python -m uvicorn app.main:app --reload --host 0.0.0.0 --port $port }