forked from mtusi2026/mtusi2026lab3
31 lines
1.1 KiB
Python
31 lines
1.1 KiB
Python
"""Внешний адрес локального стенда: BASE_URL → APP_PORT → выбранный .env."""
|
|
|
|
import os
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def base_url() -> str:
|
|
if os.environ.get("BASE_URL"):
|
|
return os.environ["BASE_URL"].rstrip("/")
|
|
port = os.environ.get("APP_PORT")
|
|
if not port:
|
|
path = Path(os.environ.get("ENV_FILE", str(ROOT / ".env")))
|
|
if not path.is_absolute():
|
|
path = ROOT / path
|
|
if path.is_file():
|
|
for line in path.read_text().splitlines():
|
|
key, separator, value = line.partition("=")
|
|
if separator and key.strip() == "APP_PORT":
|
|
port = value.split("#", 1)[0].strip().strip("\"'")
|
|
break
|
|
port = port or "8080"
|
|
if not port.isascii() or not port.isdigit() or not 1 <= int(port) <= 65535:
|
|
raise ValueError("APP_PORT должен быть номером порта от 1 до 65535")
|
|
return "http://localhost:" + str(int(port))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print(base_url())
|