fix(deploy): 初始化并校验公开 API 地址
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
#Requires -Version 5.1
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
. (Join-Path $scriptDir "Initialize-PublicApiBaseUrl.ps1")
|
||||
|
||||
$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ("easyai-public-url-" + [Guid]::NewGuid())
|
||||
[System.IO.Directory]::CreateDirectory($tempDir) | Out-Null
|
||||
|
||||
function Write-TestEnv {
|
||||
param([string]$Name, [string[]]$Lines)
|
||||
$path = Join-Path $tempDir $Name
|
||||
[System.IO.File]::WriteAllText($path, ($Lines -join "`r`n") + "`r`n", [System.Text.UTF8Encoding]::new($false))
|
||||
return $path
|
||||
}
|
||||
|
||||
function Assert-PublicUrl {
|
||||
param([string]$Path, [string]$Expected)
|
||||
$content = Get-Content $Path -Raw -Encoding UTF8
|
||||
$actual = Get-PublicEnvValue $content "CONFIG_PUBLIC_API_BASE_URL"
|
||||
if ($actual -ne $Expected) { throw "Expected '$Expected', got '$actual'" }
|
||||
}
|
||||
|
||||
try {
|
||||
$existing = Write-TestEnv "existing.env" @(
|
||||
"CONFIG_PUBLIC_API_BASE_URL=https://existing.example.com:8443/api",
|
||||
"NUXT_PUBLIC_BASE_APIURL=/api"
|
||||
)
|
||||
Initialize-PublicApiBaseUrl -Path $existing -Mode upgrade
|
||||
Assert-PublicUrl $existing "https://existing.example.com:8443/api"
|
||||
|
||||
$absolute = Write-TestEnv "absolute.env" @(
|
||||
"CONFIG_PUBLIC_API_BASE_URL=",
|
||||
"NUXT_PUBLIC_BASE_APIURL=http://10.0.0.8:4100"
|
||||
)
|
||||
Initialize-PublicApiBaseUrl -Path $absolute -Mode upgrade
|
||||
Assert-PublicUrl $absolute "http://10.0.0.8:4100"
|
||||
|
||||
$websocket = Write-TestEnv "websocket.env" @(
|
||||
"CONFIG_PUBLIC_API_BASE_URL=",
|
||||
"NUXT_PUBLIC_BASE_APIURL=/api",
|
||||
"NUXT_PUBLIC_BASE_SOCKETURL=wss://demo.example.com/socket.io"
|
||||
)
|
||||
Initialize-PublicApiBaseUrl -Path $websocket -Mode upgrade
|
||||
Assert-PublicUrl $websocket "https://demo.example.com/api"
|
||||
|
||||
$securityOrigin = Write-TestEnv "security-origin.env" @(
|
||||
"CONFIG_PUBLIC_API_BASE_URL=",
|
||||
"NUXT_PUBLIC_BASE_APIURL=/api",
|
||||
"NUXT_PUBLIC_BASE_SOCKETURL=",
|
||||
"CONFIG_SECURITY_ORIGIN=http://demo.example.com,https://demo.example.com"
|
||||
)
|
||||
Initialize-PublicApiBaseUrl -Path $securityOrigin -Mode upgrade
|
||||
Assert-PublicUrl $securityOrigin "http://demo.example.com/api"
|
||||
|
||||
$fileOverride = Write-TestEnv "file-override.env" @(
|
||||
"CONFIG_PUBLIC_API_BASE_URL=",
|
||||
"DEPLOY_PUBLIC_API_BASE_URL=https://proxy.example.com:9443/custom-api",
|
||||
"NUXT_PUBLIC_BASE_APIURL=/api"
|
||||
)
|
||||
Initialize-PublicApiBaseUrl -Path $fileOverride -Mode upgrade
|
||||
Assert-PublicUrl $fileOverride "https://proxy.example.com:9443/custom-api"
|
||||
|
||||
$explicitOverride = Write-TestEnv "explicit-override.env" @(
|
||||
"CONFIG_PUBLIC_API_BASE_URL=",
|
||||
"NUXT_PUBLIC_BASE_APIURL=/api"
|
||||
)
|
||||
Initialize-PublicApiBaseUrl `
|
||||
-Path $explicitOverride `
|
||||
-Mode configure `
|
||||
-Override "http://192.168.1.20:4567"
|
||||
Assert-PublicUrl $explicitOverride "http://192.168.1.20:4567"
|
||||
|
||||
$invalid = Write-TestEnv "invalid.env" @(
|
||||
"CONFIG_PUBLIC_API_BASE_URL=ftp://bad.example.com/api",
|
||||
"NUXT_PUBLIC_BASE_APIURL=https://good.example.com/api"
|
||||
)
|
||||
$blocked = $false
|
||||
try { Initialize-PublicApiBaseUrl -Path $invalid -Mode upgrade } catch { $blocked = $true }
|
||||
if (-not $blocked) { throw "Invalid existing CONFIG_PUBLIC_API_BASE_URL must block startup" }
|
||||
|
||||
$unresolved = Write-TestEnv "unresolved.env" @(
|
||||
"CONFIG_PUBLIC_API_BASE_URL=",
|
||||
"NUXT_PUBLIC_BASE_APIURL=/api"
|
||||
)
|
||||
$blocked = $false
|
||||
try { Initialize-PublicApiBaseUrl -Path $unresolved -Mode upgrade } catch { $blocked = $true }
|
||||
if (-not $blocked) { throw "Unresolved public API URL must block startup" }
|
||||
|
||||
Write-Host "Public API base URL PowerShell tests passed" -ForegroundColor Green
|
||||
} finally {
|
||||
Remove-Item $tempDir -Recurse -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
Reference in New Issue
Block a user