start.ps1 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # 游铛铛客户端后端启动脚本 (Windows PowerShell)
  2. $ErrorActionPreference = "Stop"
  3. $GREEN = "Green"
  4. $RED = "Red"
  5. $YELLOW = "Yellow"
  6. $CYAN = "Cyan"
  7. Write-Host "========================================" -ForegroundColor $GREEN
  8. Write-Host " 游铛铛客户端后端启动脚本" -ForegroundColor $GREEN
  9. Write-Host "========================================" -ForegroundColor $GREEN
  10. # 检查 Python
  11. try {
  12. $pythonVersion = python --version 2>&1
  13. Write-Host "[OK] Python已安装: $pythonVersion" -ForegroundColor $GREEN
  14. } catch {
  15. Write-Host "[错误] 未找到Python,请先安装Python3" -ForegroundColor $RED
  16. exit 1
  17. }
  18. # 检查是否在 backend-client 目录
  19. if (-not (Test-Path "app\main.py")) {
  20. Write-Host "[错误] 请在 backend-client 目录下运行此脚本" -ForegroundColor $RED
  21. exit 1
  22. }
  23. # 解析启动模式
  24. $Mode = "dev"
  25. if ($args[0] -eq "prod" -or $args[0] -eq "production") {
  26. $Mode = "prod"
  27. }
  28. Write-Host ""
  29. Write-Host "启动模式: $Mode" -ForegroundColor $CYAN
  30. # 检查依赖
  31. if (-not (Test-Path "venv") -and -not (Test-Path ".venv")) {
  32. Write-Host "[警告] 未找到虚拟环境" -ForegroundColor $YELLOW
  33. $install = Read-Host "是否自动创建虚拟环境并安装依赖? (Y/N)"
  34. if ($install -eq "Y" -or $install -eq "y") {
  35. Write-Host "创建虚拟环境..." -ForegroundColor $CYAN
  36. python -m venv venv
  37. Write-Host "安装依赖..." -ForegroundColor $CYAN
  38. & .\venv\Scripts\pip install -r requirements.txt
  39. }
  40. }
  41. # 检查端口是否被占用
  42. $port = 9000
  43. $connection = Get-NetTCPConnection -LocalPort $port -ErrorAction SilentlyContinue
  44. if ($connection) {
  45. Write-Host "[警告] 端口 $port 已被占用!" -ForegroundColor $YELLOW
  46. Write-Host "占用进程:"
  47. $connection | Format-Table -AutoSize
  48. $kill = Read-Host "是否杀掉占用进程并继续? (Y/N)"
  49. if ($kill -eq "Y" -or $kill -eq "y") {
  50. $pids = $connection | Select-Object -ExpandProperty OwningProcess -Unique
  51. foreach ($pid in $pids) {
  52. Stop-Process -Id $pid -Force -ErrorAction SilentlyContinue
  53. Write-Host "已终止进程 PID: $pid" -ForegroundColor $YELLOW
  54. }
  55. Start-Sleep -Seconds 1
  56. } else {
  57. Write-Host "启动取消" -ForegroundColor $RED
  58. exit 1
  59. }
  60. }
  61. Write-Host ""
  62. Write-Host "API地址: http://127.0.0.1:9000" -ForegroundColor $CYAN
  63. Write-Host "API文档: http://127.0.0.1:9000/docs" -ForegroundColor $CYAN
  64. Write-Host ""
  65. Write-Host "按 Ctrl+C 停止服务" -ForegroundColor $YELLOW
  66. Write-Host ""
  67. # 启动服务
  68. if ($Mode -eq "prod") {
  69. Write-Host "启动生产服务器 (4 workers)..." -ForegroundColor $GREEN
  70. python -m uvicorn app.main:app --host 0.0.0.0 --port $port --workers 4
  71. } else {
  72. Write-Host "启动开发服务器 (reload模式)..." -ForegroundColor $GREEN
  73. python -m uvicorn app.main:app --reload --host 0.0.0.0 --port $port
  74. }