Some checks failed
Publish container images / Quality checks (push) Has been cancelled
Publish container images / Resolve build version (push) Has been cancelled
Publish container images / Build open-ai-canvas-backend (amd64) (push) Has been cancelled
Publish container images / Build open-ai-canvas-backend (arm64) (push) Has been cancelled
Publish container images / Build open-ai-canvas-web (multi) (push) Has been cancelled
Publish container images / Build open-ai-canvas-yingce-agent (multi) (push) Has been cancelled
Publish container images / Publish open-ai-canvas-backend manifest (push) Has been cancelled
Publish container images / Publish open-ai-canvas-web manifest (push) Has been cancelled
Publish container images / Publish open-ai-canvas-yingce-agent manifest (push) Has been cancelled
Publish container images / Publish release assets (push) Has been cancelled
128 lines
5.4 KiB
PowerShell
128 lines
5.4 KiB
PowerShell
[CmdletBinding()]
|
||
param()
|
||
|
||
$ErrorActionPreference = "Stop"
|
||
|
||
. (Join-Path $PSScriptRoot "windows-proxy.ps1")
|
||
Import-CanvasWindowsProxy
|
||
|
||
$repoRoot = (Resolve-Path (Join-Path $PSScriptRoot "..")).Path
|
||
$backendDir = Join-Path $repoRoot "backend"
|
||
$webDir = Join-Path $repoRoot "web"
|
||
$dataDir = Join-Path $repoRoot ".local\project-workbench-debug"
|
||
$goBuildCache = Join-Path $repoRoot ".local\cache\go-build"
|
||
$goModuleCache = Join-Path $repoRoot ".local\cache\go-mod"
|
||
|
||
# 从仓库根 .env 加载配置(KEY=VALUE,支持 # 注释与空行;不存在则跳过)。
|
||
# 后端只读取环境变量,这里把 .env 注入进程环境,子进程(go/bun)自动继承。
|
||
$envFile = Join-Path $repoRoot ".env"
|
||
if (-not (Test-Path -LiteralPath $envFile)) {
|
||
# Windows 常见坑:在记事本等里新建文件常变成 .env.txt
|
||
$alt = Join-Path $repoRoot ".env.txt"
|
||
if (Test-Path -LiteralPath $alt) { $envFile = $alt }
|
||
else { Write-Host "未找到 $envFile,将使用内置默认值(SQLite)。如需 PostgreSQL 请先创建该文件。" -ForegroundColor Yellow }
|
||
}
|
||
if (Test-Path -LiteralPath $envFile) {
|
||
Write-Host "正在从 $envFile 加载配置..." -ForegroundColor Cyan
|
||
$loaded = @()
|
||
Get-Content -LiteralPath $envFile | ForEach-Object {
|
||
$line = $_.Trim()
|
||
if ($line -eq "" -or $line.StartsWith("#")) { return }
|
||
if ($line -match '^\s*export\s+') { $line = $line -replace '^\s*export\s+', '' }
|
||
if ($line -match '^\s*([^=]+)=(.*)$') {
|
||
$name = $matches[1].Trim()
|
||
$value = $matches[2].Trim()
|
||
if ($value.StartsWith('"') -and $value.EndsWith('"')) {
|
||
$value = $value.Substring(1, $value.Length - 2)
|
||
}
|
||
[Environment]::SetEnvironmentVariable($name, $value, [System.EnvironmentVariableTarget]::Process)
|
||
$loaded += $name
|
||
}
|
||
}
|
||
Write-Host "已加载变量:$($loaded -join ', ')" -ForegroundColor Cyan
|
||
if (-not $env:DATABASE_URL) { Write-Host "警告:DATABASE_URL 为空,后端将回退 SQLite(数据不会同步)。" -ForegroundColor Yellow }
|
||
}
|
||
# 本地绑定默认值:未显式配置时,后端只监听本机、数据落在本地调试目录
|
||
if (-not $env:CANVAS_BACKEND_ADDR) { $env:CANVAS_BACKEND_ADDR = '127.0.0.1:7381' }
|
||
if (-not $env:CANVAS_BACKEND_DATA_DIR) { $env:CANVAS_BACKEND_DATA_DIR = $dataDir }
|
||
|
||
foreach ($commandName in @("go", "bun")) {
|
||
if (-not (Get-Command $commandName -ErrorAction SilentlyContinue)) {
|
||
throw "未找到 $commandName,请先安装项目要求的运行时。"
|
||
}
|
||
}
|
||
|
||
foreach ($directory in @($dataDir, $goBuildCache, $goModuleCache)) {
|
||
New-Item -ItemType Directory -Force -Path $directory | Out-Null
|
||
}
|
||
|
||
$viteBinary = Join-Path $webDir "node_modules\.bin\vite"
|
||
if (-not (Test-Path -LiteralPath $viteBinary)) {
|
||
Write-Host "web/node_modules 不存在,正在执行 bun install --frozen-lockfile..." -ForegroundColor Yellow
|
||
Push-Location $webDir
|
||
try {
|
||
& bun install --frozen-lockfile
|
||
if ($LASTEXITCODE -ne 0) {
|
||
throw "bun install 失败,无法启动前端。"
|
||
}
|
||
} finally {
|
||
Pop-Location
|
||
}
|
||
}
|
||
|
||
function Test-ListeningPort([int]$Port) {
|
||
try {
|
||
return $null -ne (Get-NetTCPConnection -LocalPort $Port -State Listen -ErrorAction Stop | Select-Object -First 1)
|
||
} catch {
|
||
return $false
|
||
}
|
||
}
|
||
|
||
# 本项目本地开发端口为前端 7380 / 后端 7381(见 AGENTS.md「本项目附加约定」)。
|
||
# 上游默认的 3000 / 8080 在同机上被其它工程占用,不能作为探测目标。
|
||
foreach ($port in @(7380, 7381)) {
|
||
if (Test-ListeningPort $port) {
|
||
throw "端口 $port 已被占用,请先关闭占用进程后重试。"
|
||
}
|
||
}
|
||
|
||
$powerShellPath = (Get-Command pwsh -ErrorAction SilentlyContinue).Source
|
||
if (-not $powerShellPath) {
|
||
$powerShellPath = (Get-Command powershell.exe -ErrorAction SilentlyContinue).Source
|
||
}
|
||
if (-not $powerShellPath) {
|
||
throw "未找到 PowerShell,无法打开前后端独立窗口。"
|
||
}
|
||
|
||
function ConvertTo-PowerShellLiteral([string]$Value) {
|
||
return "'" + $Value.Replace("'", "''") + "'"
|
||
}
|
||
|
||
$backendDirLiteral = ConvertTo-PowerShellLiteral $backendDir
|
||
$webDirLiteral = ConvertTo-PowerShellLiteral $webDir
|
||
$dataDirLiteral = ConvertTo-PowerShellLiteral $dataDir
|
||
|
||
$backendCommand = @"
|
||
`$ErrorActionPreference = 'Stop'
|
||
Set-Location -LiteralPath $backendDirLiteral
|
||
`$env:GOCACHE = $(ConvertTo-PowerShellLiteral $goBuildCache)
|
||
`$env:GOMODCACHE = $(ConvertTo-PowerShellLiteral $goModuleCache)
|
||
Write-Host "影策后端:$($env:CANVAS_BACKEND_ADDR)" -ForegroundColor Cyan
|
||
go run ./cmd/server
|
||
"@
|
||
|
||
$webCommand = @"
|
||
`$ErrorActionPreference = 'Stop'
|
||
Set-Location -LiteralPath $webDirLiteral
|
||
`$env:VITE_API_PROXY_TARGET = 'http://127.0.0.1:7381'
|
||
Write-Host '影策前端:http://localhost:7380' -ForegroundColor Cyan
|
||
bun run dev
|
||
"@
|
||
|
||
$backendProcess = Start-Process -FilePath $powerShellPath -WindowStyle Normal -WorkingDirectory $backendDir -PassThru -ArgumentList @("-NoLogo", "-NoExit", "-NoProfile", "-Command", $backendCommand)
|
||
$webProcess = Start-Process -FilePath $powerShellPath -WindowStyle Normal -WorkingDirectory $webDir -PassThru -ArgumentList @("-NoLogo", "-NoExit", "-NoProfile", "-Command", $webCommand)
|
||
|
||
Write-Host "已打开前后端开发窗口。" -ForegroundColor Green
|
||
Write-Host "后端窗口 PID: $($backendProcess.Id);前端窗口 PID: $($webProcess.Id)"
|
||
Write-Host "访问 http://localhost:7380;分别在两个窗口按 Ctrl+C 停止服务。"
|