Files
any-auto-register/start_backend.ps1
2026-03-28 13:02:31 +08:00

40 lines
1.2 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
param(
[string]$EnvName = "any-auto-register",
[string]$BindHost = "0.0.0.0",
[int]$Port = 8000,
[switch]$RestartExisting = $true
)
$ErrorActionPreference = "Stop"
$root = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location $root
$conda = Get-Command conda -ErrorAction SilentlyContinue
if (-not $conda) {
Write-Error "未找到 conda 命令。请先安装 Miniconda/Anaconda并确保 conda 可在终端中使用。"
exit 1
}
Write-Host "[INFO] 项目目录: $root"
Write-Host "[INFO] 使用 conda 环境: $EnvName"
$displayHost = if ($BindHost -eq "0.0.0.0") { "localhost" } else { $BindHost }
Write-Host "[INFO] 启动后端: http://$displayHost`:$Port"
Write-Host "[INFO] 按 Ctrl+C 可停止服务"
if ($RestartExisting) {
Write-Host "[INFO] 启动前先清理旧的后端 / Solver 进程"
& "$root\stop_backend.ps1" -BackendPort $Port -SolverPort 8889 -FullStop 0
}
$pythonExe = (conda run --no-capture-output -n $EnvName python -c "import sys; print(sys.executable)").Trim()
if (-not (Test-Path $pythonExe)) {
Write-Error "无法解析 conda 环境 '$EnvName' 对应的 python 路径。"
exit 1
}
$env:HOST = $BindHost
$env:PORT = [string]$Port
Write-Host "[INFO] Python: $pythonExe"
& $pythonExe main.py