$root = "D:\lq\Smart Parking\lc_garage" $exe = Join-Path $root "backend-only.exe" # 清理老进程 Get-Process -Name "backend-only" -ErrorAction SilentlyContinue | Stop-Process -Force Get-Process -Name "smart-parking-test" -ErrorAction SilentlyContinue | Stop-Process -Force # 编译 Write-Host "=== 编译后端 ===" cd $root go build -o backend-only.exe .\cmd_backend_only\ 2>&1 if ($LASTEXITCODE -ne 0) { Write-Host "编译失败!" exit 1 } Write-Host "编译成功" # 启动服务器(后台作业) $job = Start-Job -Name "server" -ScriptBlock { param($exe, $root) Set-Location $root & $exe 2>&1 | Out-File (Join-Path $root "server_job.txt") } -ArgumentList $exe, $root # 等服务器启动 Write-Host "等待服务启动..." $ready = $false for ($i = 0; $i -lt 30; $i++) { Start-Sleep -Seconds 1 try { $r = Invoke-WebRequest -Uri "http://127.0.0.1:8888/health" -UseBasicParsing -TimeoutSec 2 -ErrorAction Stop if ($r.StatusCode -eq 200) { $ready = $true Write-Host "服务已就绪!" break } } catch { # 还没好 } } if (-not $ready) { Write-Host "服务启动超时" Get-Content (Join-Path $root "server_job.txt") -ErrorAction SilentlyContinue Stop-Job $job -ErrorAction SilentlyContinue Remove-Job $job -ErrorAction SilentlyContinue exit 1 } # 测试接口 Write-Host "`n=== 测试健康检查 ===" $health = Invoke-WebRequest -Uri "http://127.0.0.1:8888/health" -UseBasicParsing Write-Host ("状态码: " + $health.StatusCode) Write-Host ("响应: " + $health.Content) Write-Host "`n=== 测试验证码接口 ===" try { $captcha = Invoke-WebRequest -Uri "http://127.0.0.1:8888/base/captcha" -UseBasicParsing -Method POST Write-Host ("状态码: " + $captcha.StatusCode) Write-Host ("响应: " + $captcha.Content.Substring(0, [Math]::Min(200, $captcha.Content.Length))) } catch { Write-Host ("验证码接口: " + $_.Exception.Message) } Write-Host "`n=== 服务验证通过 ✅ ===" # 清理 Stop-Job $job -ErrorAction SilentlyContinue Remove-Job $job -ErrorAction SilentlyContinue