Files
ProxyPool/script/start.ps1
2026-01-27 21:17:36 +08:00

110 lines
4.0 KiB
PowerShell

# ProxyPool Startup Script
$rootPath = Split-Path $PSScriptRoot -Parent
Write-Host "=== ProxyPool Startup ===" -ForegroundColor Cyan
Write-Host ""
# 1. Clean processes on ports 8923 and 6173
Write-Host "[1/4] Cleaning old processes..." -ForegroundColor Cyan
$ports = @(8923, 6173)
foreach ($port in $ports) {
try {
$conn = Get-NetTCPConnection -LocalPort $port -ErrorAction SilentlyContinue
if ($conn) {
$processId = $conn.OwningProcess
Stop-Process -Id $processId -Force -ErrorAction SilentlyContinue
Write-Host " Stopped port $port (PID: $processId)" -ForegroundColor Gray
}
} catch {}
}
Write-Host " Cleanup complete!" -ForegroundColor Green
Write-Host ""
# 2. Start Backend (FastAPI)
Write-Host "[2/4] Starting backend (FastAPI)..." -ForegroundColor Cyan
$venvPython = "$rootPath\venv\Scripts\python.exe"
if (Test-Path $venvPython) {
$pythonPath = $venvPython
Write-Host " Using venv: $venvPython" -ForegroundColor Green
} else {
$pythonPath = (Get-Command python).Source
Write-Host " Using system Python: $pythonPath" -ForegroundColor Yellow
}
$env:PYTHONIOENCODING = "utf-8"
$backendLog = "$rootPath\logs\backend_startup.log"
$backendErr = "$rootPath\logs\backend_error.log"
# Clear old logs
if (Test-Path $backendLog) { Remove-Item $backendLog -Force }
if (Test-Path $backendErr) { Remove-Item $backendErr -Force }
# Start backend with -u flag for unbuffered output and redirect logs
$backendProcess = Start-Process -FilePath $pythonPath -ArgumentList "-u", "api_server.py" -WorkingDirectory "$rootPath" -RedirectStandardOutput $backendLog -RedirectStandardError $backendErr -WindowStyle Hidden -PassThru
Write-Host " Backend started (PID: $($backendProcess.Id))" -ForegroundColor Green
Write-Host ""
# 3. Wait for backend to be ready (max 10 seconds)
Write-Host "[3/4] Waiting for backend..." -ForegroundColor Cyan
$maxRetries = 5
$retryCount = 0
$backendReady = $false
while (-not $backendReady -and $retryCount -lt $maxRetries) {
Start-Sleep -Seconds 2
$retryCount++
try {
$response = Invoke-RestMethod -Uri "http://127.0.0.1:8923/" -Method Get -TimeoutSec 2 -ErrorAction Stop
if ($response) {
$backendReady = $true
Write-Host " Backend is ready!" -ForegroundColor Green
}
} catch {
$errMessage = $_.Exception.Message
Write-Host " Waiting... ($retryCount/$maxRetries)" -ForegroundColor Yellow
if (Test-Path $backendLog) {
$lastLog = Get-Content $backendLog -Tail 1 -ErrorAction SilentlyContinue
if ($lastLog) { Write-Host " Log: $lastLog" -ForegroundColor DarkGray }
}
if ($backendProcess.HasExited) {
Write-Host " Backend process exited!" -ForegroundColor Red
Write-Host " Exit code: $($backendProcess.ExitCode)" -ForegroundColor Red
if (Test-Path $backendErr) {
Write-Host "" -ForegroundColor Red
Write-Host "Error log:" -ForegroundColor Red
Get-Content $backendErr -Tail 20 | ForEach-Object { Write-Host " $_" -ForegroundColor Red }
}
$backendReady = $false
break
}
}
}
if (-not $backendReady) {
Write-Host "" -ForegroundColor Red
Write-Host "Backend failed to start!" -ForegroundColor Red
Write-Host "Check error log: $backendErr" -ForegroundColor Red
pause
exit
}
Write-Host ""
# 4. Start Frontend (Vite)
Write-Host "[4/4] Starting frontend (Vite)..." -ForegroundColor Cyan
Start-Process -FilePath "cmd" -ArgumentList "/c npm run dev" -WorkingDirectory "$rootPath\frontend" -WindowStyle Hidden
Write-Host " Frontend started" -ForegroundColor Green
Write-Host ""
Write-Host "=== All services started ===" -ForegroundColor Cyan
Write-Host "Backend: http://127.0.0.1:8923" -ForegroundColor Green
Write-Host "Frontend: http://localhost:6173" -ForegroundColor Green
Write-Host ""
Write-Host "Please open frontend in browser" -ForegroundColor Magenta