fix(gateway): treat a blank GATEWAY_HOST/GATEWAY_PORT as unset (#5801)

os.getenv's default only covers an unset variable, so `docker run -e
GATEWAY_PORT=` — or `GATEWAY_PORT=${PORT}` with PORT unset — reached
int("") and aborted the create_app() import that both entry points run.
Blank now falls back to the declared default, while "0" stays a real port.
This commit is contained in:
Grapette.L 2026-09-24 15:56:38 +08:00 committed by GitHub
parent 7833801690
commit 2c43767bbe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 2 deletions

View File

@ -19,8 +19,9 @@ def get_gateway_config() -> GatewayConfig:
global _gateway_config
if _gateway_config is None:
_gateway_config = GatewayConfig(
host=os.getenv("GATEWAY_HOST", "0.0.0.0"),
port=int(os.getenv("GATEWAY_PORT", "8001")),
# A blank means "unset" (`-e GATEWAY_PORT=`, or `${PORT}` with PORT unset); int("") would abort the create_app() import
host=os.getenv("GATEWAY_HOST") or "0.0.0.0",
port=int(os.getenv("GATEWAY_PORT") or "8001"),
enable_docs=os.getenv("GATEWAY_ENABLE_DOCS", "true").lower() == "true",
)
return _gateway_config

View File

@ -78,6 +78,31 @@ def test_enable_docs_unexpected_value_disables():
assert config.enable_docs is False, f"Expected False for GATEWAY_ENABLE_DOCS={value}"
def test_blank_host_and_port_fall_back_to_defaults():
"""A blank GATEWAY_HOST/GATEWAY_PORT means unset, not a crash or an empty bind address.
``docker run -e GATEWAY_PORT=`` and ``environment: [GATEWAY_PORT=${PORT}]`` with PORT
unset both reach us as an empty string, and create_app() reads this at import time.
"""
with patch.dict(os.environ, {"GATEWAY_HOST": "", "GATEWAY_PORT": ""}):
_reset_gateway_config()
from app.gateway.config import get_gateway_config
config = get_gateway_config()
assert config.port == 8001
assert config.host == "0.0.0.0"
def test_zero_port_is_not_treated_as_unset():
"""GATEWAY_PORT=0 asks the OS for a free port, so it must survive the blank fallback."""
with patch.dict(os.environ, {"GATEWAY_PORT": "0"}):
_reset_gateway_config()
from app.gateway.config import get_gateway_config
config = get_gateway_config()
assert config.port == 0
# ---------------------------------------------------------------------------
# App-level endpoint visibility
# ---------------------------------------------------------------------------