start_local_static.ps1 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # Local static JSON HTTP servers for AccountServer config fetch
  2. # Run 静态服务启动.bat from repo root (admin recommended for port 80)
  3. $root = Split-Path -Parent $PSScriptRoot
  4. $port80Dir = Join-Path $root "local_static\port80"
  5. $port8000Dir = Join-Path $root "local_static\port8000"
  6. $logDir = Join-Path $root "local_static\logs"
  7. $pidDir = Join-Path $root "local_static\pids"
  8. New-Item -ItemType Directory -Force -Path $logDir, $pidDir, $port80Dir, $port8000Dir | Out-Null
  9. function Get-PythonExe {
  10. $cmd = Get-Command python -ErrorAction SilentlyContinue
  11. if ($cmd) { return $cmd.Source }
  12. throw "python not found in PATH"
  13. }
  14. function Test-PortListening([int]$Port) {
  15. $conn = Get-NetTCPConnection -LocalPort $Port -State Listen -ErrorAction SilentlyContinue
  16. return $null -ne $conn
  17. }
  18. function Stop-StaticServer([int]$Port) {
  19. $pidFile = Join-Path $pidDir "http_$Port.pid"
  20. if (-not (Test-Path $pidFile)) { return }
  21. $oldPid = [int](Get-Content $pidFile -ErrorAction SilentlyContinue)
  22. if ($oldPid -gt 0) {
  23. $proc = Get-Process -Id $oldPid -ErrorAction SilentlyContinue
  24. if ($proc) {
  25. Write-Host "[stop] port $Port old pid $oldPid"
  26. Stop-Process -Id $oldPid -Force -ErrorAction SilentlyContinue
  27. Start-Sleep -Milliseconds 500
  28. }
  29. }
  30. Remove-Item $pidFile -Force -ErrorAction SilentlyContinue
  31. }
  32. function Start-StaticServer([int]$Port, [string]$Directory) {
  33. $python = Get-PythonExe
  34. $logFile = Join-Path $logDir "http_$Port.log"
  35. $errFile = Join-Path $logDir "http_$Port.err.log"
  36. $pidFile = Join-Path $pidDir "http_$Port.pid"
  37. if (Test-PortListening $Port) {
  38. if (Test-Path $pidFile) {
  39. $existingPid = [int](Get-Content $pidFile -ErrorAction SilentlyContinue)
  40. $proc = Get-Process -Id $existingPid -ErrorAction SilentlyContinue
  41. if ($proc) {
  42. Write-Host "[skip] port $Port already running (pid $existingPid)"
  43. return $true
  44. }
  45. }
  46. Write-Host "[warn] port $Port is used by another process, trying to restart managed service"
  47. }
  48. Stop-StaticServer $Port
  49. $argList = "-m http.server $Port --bind 127.0.0.1 --directory `"$Directory`""
  50. Write-Host "[start] port $Port -> $Directory"
  51. try {
  52. $proc = Start-Process -FilePath $python `
  53. -ArgumentList $argList `
  54. -WindowStyle Hidden `
  55. -RedirectStandardOutput $logFile `
  56. -RedirectStandardError $errFile `
  57. -PassThru
  58. } catch {
  59. Write-Host "[fail] port $Port start error: $($_.Exception.Message)"
  60. return $false
  61. }
  62. $proc.Id | Out-File -FilePath $pidFile -Encoding ascii -NoNewline
  63. Start-Sleep -Seconds 1
  64. if ($proc.HasExited) {
  65. Write-Host "[fail] port $Port exited immediately, see $logFile"
  66. return $false
  67. }
  68. if (-not (Test-PortListening $Port)) {
  69. Write-Host "[fail] port $Port not listening, see $logFile"
  70. return $false
  71. }
  72. Write-Host "[ok] port $Port pid $($proc.Id)"
  73. return $true
  74. }
  75. $ok80 = Start-StaticServer -Port 80 -Directory $port80Dir
  76. $ok8000 = Start-StaticServer -Port 8000 -Directory $port8000Dir
  77. Write-Host ""
  78. Write-Host "verify:"
  79. foreach ($item in @(
  80. @{ Url = "http://127.0.0.1/cjw_60000_stop_server.json"; Ok = $ok80 },
  81. @{ Url = "http://127.0.0.1:8000/serverlist.json"; Ok = $ok8000 }
  82. )) {
  83. if (-not $item.Ok) {
  84. Write-Host " FAIL $($item.Url) (service not started)"
  85. continue
  86. }
  87. try {
  88. $r = Invoke-WebRequest -Uri $item.Url -UseBasicParsing -TimeoutSec 5
  89. Write-Host " OK $($item.Url) HTTP $($r.StatusCode)"
  90. } catch {
  91. Write-Host " FAIL $($item.Url) $($_.Exception.Message)"
  92. }
  93. }
  94. if (-not $ok80 -or -not $ok8000) {
  95. Write-Host ""
  96. Write-Host "tips:"
  97. Write-Host " - run bat as Administrator if port 80 fails"
  98. Write-Host " - check logs in local_static\logs\"
  99. Write-Host " - run 静态服务停止.bat then start again"
  100. exit 1
  101. }