forked from wangbo/easyai
48 lines
1.9 KiB
PowerShell
48 lines
1.9 KiB
PowerShell
function Get-ServerHttpBindEnvValue {
|
|
param([string]$Content, [string]$Key)
|
|
$match = [regex]::Match($Content, "(?m)^$([regex]::Escape($Key))=([^\r\n]*)$")
|
|
if ($match.Success) { return $match.Groups[1].Value.Trim() }
|
|
return ""
|
|
}
|
|
|
|
function Set-ServerHttpBindEnvValue {
|
|
param([string]$Content, [string]$Key, [string]$Value)
|
|
$line = "$Key=$Value"
|
|
$pattern = "(?m)^$([regex]::Escape($Key))=.*$"
|
|
if ($Content -match $pattern) { return ($Content -replace $pattern, $line) }
|
|
if ($Content -and -not $Content.EndsWith("`n")) { $Content += "`n" }
|
|
return ($Content + $line + "`n")
|
|
}
|
|
|
|
function Initialize-ServerHttpBindIp {
|
|
param(
|
|
[Parameter(Mandatory = $true)][string]$Path,
|
|
[string]$Override = ""
|
|
)
|
|
|
|
if (-not (Test-Path $Path)) { throw "Environment file not found: $Path" }
|
|
$content = Get-Content $Path -Raw -Encoding UTF8
|
|
if (-not $content) { $content = "" }
|
|
|
|
$current = Get-ServerHttpBindEnvValue $content "SERVER_HTTP_BIND_IP"
|
|
$target = if (-not [string]::IsNullOrWhiteSpace($Override)) { $Override.Trim() } else { $current }
|
|
if ([string]::IsNullOrWhiteSpace($target)) {
|
|
$publicApiUrl = Get-ServerHttpBindEnvValue $content "NUXT_PUBLIC_BASE_APIURL"
|
|
$target = if (
|
|
$publicApiUrl -eq "/api" -or
|
|
$publicApiUrl.StartsWith("/api/") -or
|
|
$publicApiUrl -match '^https?://(127\.0\.0\.1|localhost)(:\d+)?(?:/|$)'
|
|
) { "127.0.0.1" } elseif ($publicApiUrl -match '^https?://') { "0.0.0.0" } else { "127.0.0.1" }
|
|
}
|
|
|
|
if ($target -notin @("127.0.0.1", "0.0.0.0")) {
|
|
throw "SERVER_HTTP_BIND_IP only supports 127.0.0.1 or 0.0.0.0, current value: $target"
|
|
}
|
|
|
|
if ($current -ne $target) {
|
|
$content = Set-ServerHttpBindEnvValue $content "SERVER_HTTP_BIND_IP" $target
|
|
[System.IO.File]::WriteAllText($Path, $content, [System.Text.UTF8Encoding]::new($false))
|
|
Write-Host " ✓ SERVER_HTTP_BIND_IP=$target"
|
|
}
|
|
}
|