test_server.ps1 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. $root = "D:\lq\Smart Parking\lc_garage"
  2. $exe = Join-Path $root "backend-only.exe"
  3. # 清理老进程
  4. Get-Process -Name "backend-only" -ErrorAction SilentlyContinue | Stop-Process -Force
  5. Get-Process -Name "smart-parking-test" -ErrorAction SilentlyContinue | Stop-Process -Force
  6. # 编译
  7. Write-Host "=== 编译后端 ==="
  8. cd $root
  9. go build -o backend-only.exe .\cmd_backend_only\ 2>&1
  10. if ($LASTEXITCODE -ne 0) {
  11. Write-Host "编译失败!"
  12. exit 1
  13. }
  14. Write-Host "编译成功"
  15. # 启动服务器(后台作业)
  16. $job = Start-Job -Name "server" -ScriptBlock {
  17. param($exe, $root)
  18. Set-Location $root
  19. & $exe 2>&1 | Out-File (Join-Path $root "server_job.txt")
  20. } -ArgumentList $exe, $root
  21. # 等服务器启动
  22. Write-Host "等待服务启动..."
  23. $ready = $false
  24. for ($i = 0; $i -lt 30; $i++) {
  25. Start-Sleep -Seconds 1
  26. try {
  27. $r = Invoke-WebRequest -Uri "http://127.0.0.1:8888/health" -UseBasicParsing -TimeoutSec 2 -ErrorAction Stop
  28. if ($r.StatusCode -eq 200) {
  29. $ready = $true
  30. Write-Host "服务已就绪!"
  31. break
  32. }
  33. } catch {
  34. # 还没好
  35. }
  36. }
  37. if (-not $ready) {
  38. Write-Host "服务启动超时"
  39. Get-Content (Join-Path $root "server_job.txt") -ErrorAction SilentlyContinue
  40. Stop-Job $job -ErrorAction SilentlyContinue
  41. Remove-Job $job -ErrorAction SilentlyContinue
  42. exit 1
  43. }
  44. # 测试接口
  45. Write-Host "`n=== 测试健康检查 ==="
  46. $health = Invoke-WebRequest -Uri "http://127.0.0.1:8888/health" -UseBasicParsing
  47. Write-Host ("状态码: " + $health.StatusCode)
  48. Write-Host ("响应: " + $health.Content)
  49. Write-Host "`n=== 测试验证码接口 ==="
  50. try {
  51. $captcha = Invoke-WebRequest -Uri "http://127.0.0.1:8888/base/captcha" -UseBasicParsing -Method POST
  52. Write-Host ("状态码: " + $captcha.StatusCode)
  53. Write-Host ("响应: " + $captcha.Content.Substring(0, [Math]::Min(200, $captcha.Content.Length)))
  54. } catch {
  55. Write-Host ("验证码接口: " + $_.Exception.Message)
  56. }
  57. Write-Host "`n=== 服务验证通过 ✅ ==="
  58. # 清理
  59. Stop-Job $job -ErrorAction SilentlyContinue
  60. Remove-Job $job -ErrorAction SilentlyContinue