37 lines
1.5 KiB
PowerShell
37 lines
1.5 KiB
PowerShell
$ErrorActionPreference = 'Stop'
|
|
$root = Split-Path -Parent $MyInvocation.MyCommand.Path
|
|
$python = Join-Path $root 'domainCheck\.venv\Scripts\python.exe'
|
|
$apiRoot = Join-Path $root 'domain-api'
|
|
$runtimeLogDir = Join-Path $apiRoot 'runtime\logs'
|
|
$stdoutLog = Join-Path $runtimeLogDir 'domain-api.stdout.log'
|
|
$stderrLog = Join-Path $runtimeLogDir 'domain-api.stderr.log'
|
|
$apiHost = if ($env:API_HOST) { $env:API_HOST } else { '0.0.0.0' }
|
|
$apiPort = if ($env:API_PORT) { $env:API_PORT } else { '8100' }
|
|
|
|
if (-not (Test-Path $python)) {
|
|
throw "未找到 Python 解释器: $python"
|
|
}
|
|
|
|
New-Item -ItemType Directory -Force -Path $runtimeLogDir | Out-Null
|
|
|
|
$processIds = @()
|
|
$targets = Get-CimInstance Win32_Process | Where-Object { $_.CommandLine -like "*app.main:app*--port $apiPort*" }
|
|
$processIds += $targets.ProcessId
|
|
$listeners = Get-NetTCPConnection -LocalPort ([int]$apiPort) -State Listen -ErrorAction SilentlyContinue
|
|
if ($listeners) {
|
|
$processIds += $listeners.OwningProcess
|
|
}
|
|
$processIds = $processIds | Where-Object { $_ } | Select-Object -Unique
|
|
foreach ($processId in $processIds) {
|
|
Stop-Process -Id $processId -Force -ErrorAction SilentlyContinue
|
|
}
|
|
|
|
Set-Location $apiRoot
|
|
Start-Process -FilePath $python `
|
|
-ArgumentList '-m', 'uvicorn', 'app.main:app', '--host', $apiHost, '--port', $apiPort `
|
|
-WorkingDirectory $apiRoot `
|
|
-RedirectStandardOutput $stdoutLog `
|
|
-RedirectStandardError $stderrLog
|
|
|
|
Write-Output "domain-api 已启动(${apiHost}:${apiPort}),日志:$stdoutLog / $stderrLog"
|