$ErrorActionPreference = 'Stop' Set-Location -LiteralPath $PSScriptRoot $env:PYTHONIOENCODING = 'utf-8' $env:PYTHONUTF8 = '1' $ApiHost = '127.0.0.1' $ApiPort = 8765 $WebPort = 5173 $PageUrl = "http://${ApiHost}:${WebPort}" $ApiUrl = "http://${ApiHost}:${ApiPort}" $Python = Join-Path $PSScriptRoot '.venv-web\Scripts\python.exe' $WebDir = Join-Path $PSScriptRoot 'web' function Test-Listening([int] $Port) { $pattern = ":$Port\s+" netstat -ano | Where-Object { $_ -match $pattern -and $_ -match 'LISTENING' } } function Stop-Tree($Proc) { if (-not $Proc) { return } $children = @(Get-CimInstance Win32_Process -Filter "ParentProcessId=$($Proc.Id)" -ErrorAction SilentlyContinue) foreach ($child in $children) { $childProc = Get-Process -Id $child.ProcessId -ErrorAction SilentlyContinue if ($childProc) { Stop-Tree $childProc } } if (-not $Proc.HasExited) { Stop-Process -Id $Proc.Id -Force -ErrorAction SilentlyContinue } } function Stop-ListenerIfOurs([int] $Port, [scriptblock] $Belongs) { foreach ($line in @(Test-Listening $Port)) { if ($line -notmatch '\s(\d+)\s*$') { continue } $procId = [int]$Matches[1] if ($procId -le 0) { continue } $owner = Get-CimInstance Win32_Process -Filter "ProcessId=$procId" -ErrorAction SilentlyContinue $cmd = if ($owner) { $owner.CommandLine } else { '' } if (-not $cmd -or -not (& $Belongs $cmd)) { throw "端口 $Port 被其他程序占用,无法启动。" } Stop-Process -Id $procId -Force -ErrorAction SilentlyContinue } } function Stop-ExistingWeb { $root = $PSScriptRoot $web = Join-Path $root 'web' Get-CimInstance Win32_Process | Where-Object { $_.CommandLine -and $_.ProcessId -ne $PID -and ( ($_.Name -match 'python' -and $_.CommandLine -match 'api\.app' -and $_.CommandLine -match [regex]::Escape($root)) -or ($_.Name -match 'node' -and $_.CommandLine -match 'vite' -and $_.CommandLine -match [regex]::Escape($web)) ) } | ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue } Stop-ListenerIfOurs $ApiPort { param($cmd) $cmd -match 'api\.app' -and $cmd -match [regex]::Escape($PSScriptRoot) } Stop-ListenerIfOurs $WebPort { param($cmd) $cmd -match 'vite' -and $cmd -match [regex]::Escape($WebDir) } $deadline = (Get-Date).AddSeconds(10) do { if (-not (Test-Listening $ApiPort) -and -not (Test-Listening $WebPort)) { return } Start-Sleep -Milliseconds 200 } while ((Get-Date) -lt $deadline) throw "端口 $ApiPort 或 $WebPort 仍被占用,无法启动。" } function Wait-Port([int] $Port, $Proc, [string] $Name) { $deadline = (Get-Date).AddSeconds(90) while ((Get-Date) -lt $deadline) { if ($Proc.HasExited) { throw "$Name 进程已退出,启动失败。" } if (Test-Listening $Port) { return } Start-Sleep -Milliseconds 300 } throw "等待${Name}启动超时。" } function Start-Tool([string] $File, [string] $Arguments, [string] $Workdir) { $psi = New-Object System.Diagnostics.ProcessStartInfo $psi.FileName = $File $psi.Arguments = $Arguments $psi.WorkingDirectory = $Workdir $psi.UseShellExecute = $false return [System.Diagnostics.Process]::Start($psi) } function Ensure-WebEnvironment { if (Get-Command uv -ErrorAction SilentlyContinue) { if (-not (Test-Path -LiteralPath $Python)) { uv venv .venv-web --python 3.13 if ($LASTEXITCODE -ne 0) { throw '创建 Python 环境失败。' } } uv pip install --python $Python -r requirements-web.txt if ($LASTEXITCODE -ne 0) { throw '安装 Python 依赖失败。' } return } if (-not (Test-Path -LiteralPath $Python)) { python -m venv .venv-web if ($LASTEXITCODE -ne 0) { throw '创建 Python 环境失败。' } } & $Python -m pip install -r requirements-web.txt if ($LASTEXITCODE -ne 0) { throw '安装 Python 依赖失败。' } } if (-not (Get-Command node -ErrorAction SilentlyContinue) -or -not (Get-Command npm.cmd -ErrorAction SilentlyContinue)) { throw '未找到 Node.js 或 npm,无法启动 Vue 界面。请先安装 Node.js 后再运行 start-web。' } Stop-ExistingWeb Ensure-WebEnvironment Push-Location -LiteralPath $WebDir try { & npm.cmd install --no-audit --no-fund if ($LASTEXITCODE -ne 0) { throw '安装前端依赖失败。' } } finally { Pop-Location } Write-Host '' Write-Host '正在启动 MarketGrid Vue 对比界面' Write-Host "页面:$PageUrl" Write-Host "接口:$ApiUrl" Write-Host 'Streamlit 工具箱仍使用 start-ui.cmd(http://127.0.0.1:8501)' Write-Host '关闭本窗口会停止页面和接口。' Write-Host '' $npm = (Get-Command npm.cmd).Source $api = Start-Tool $Python '-m uvicorn api.app:app --host 127.0.0.1 --port 8765 --log-level info' $PSScriptRoot try { Wait-Port $ApiPort $api '接口' $web = Start-Tool $npm 'run dev' $WebDir try { Wait-Port $WebPort $web '页面' Write-Host "已启动:$PageUrl" Start-Process $PageUrl while (-not $api.HasExited -and -not $web.HasExited) { Start-Sleep -Milliseconds 400 } if ($api.HasExited) { $code = $api.ExitCode } else { $code = $web.ExitCode } exit $code } finally { Stop-Tree $web } } finally { Stop-Tree $api }