fix Coolify host env handling
All checks were successful
Build and Push Docker Image / build (push) Successful in 41s
All checks were successful
Build and Push Docker Image / build (push) Successful in 41s
This commit is contained in:
@@ -12,6 +12,7 @@ https://docs.djangoproject.com/en/5.2/ref/settings/
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
from urllib.parse import urlparse
|
||||
|
||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||
@@ -40,7 +41,7 @@ def env_bool(name: str, default: bool = False) -> bool:
|
||||
|
||||
def env_list(name: str, default: list[str]) -> list[str]:
|
||||
value = os.getenv(name)
|
||||
if value is None:
|
||||
if value is None or not value.strip():
|
||||
return default
|
||||
return [item.strip() for item in value.split(",") if item.strip()]
|
||||
|
||||
@@ -56,6 +57,82 @@ def env_pair(name: str, default: tuple[str, str]) -> tuple[str, str]:
|
||||
return parts[0], parts[1]
|
||||
|
||||
|
||||
def merge_unique(*value_lists: list[str]) -> list[str]:
|
||||
merged: list[str] = []
|
||||
seen: set[str] = set()
|
||||
|
||||
for values in value_lists:
|
||||
for value in values:
|
||||
if value and value not in seen:
|
||||
seen.add(value)
|
||||
merged.append(value)
|
||||
|
||||
return merged
|
||||
|
||||
|
||||
def normalize_host(value: str) -> str | None:
|
||||
value = value.strip()
|
||||
if not value:
|
||||
return None
|
||||
|
||||
parsed = urlparse(value if "://" in value else f"//{value}")
|
||||
if parsed.hostname:
|
||||
return parsed.hostname
|
||||
|
||||
return value.split("/", 1)[0].split(":", 1)[0] or None
|
||||
|
||||
|
||||
def normalize_origin(value: str) -> str | None:
|
||||
value = value.strip()
|
||||
if not value:
|
||||
return None
|
||||
|
||||
if "://" not in value:
|
||||
host = normalize_host(value)
|
||||
return f"https://{host}" if host else None
|
||||
|
||||
parsed = urlparse(value)
|
||||
if not parsed.scheme or not parsed.hostname:
|
||||
return None
|
||||
|
||||
origin = f"{parsed.scheme}://{parsed.hostname}"
|
||||
if parsed.port:
|
||||
origin = f"{origin}:{parsed.port}"
|
||||
return origin
|
||||
|
||||
|
||||
def collect_hosts(*env_names: str) -> list[str]:
|
||||
hosts: list[str] = []
|
||||
|
||||
for env_name in env_names:
|
||||
value = os.getenv(env_name)
|
||||
if not value:
|
||||
continue
|
||||
|
||||
for item in value.split(","):
|
||||
host = normalize_host(item)
|
||||
if host:
|
||||
hosts.append(host)
|
||||
|
||||
return hosts
|
||||
|
||||
|
||||
def collect_origins(*env_names: str) -> list[str]:
|
||||
origins: list[str] = []
|
||||
|
||||
for env_name in env_names:
|
||||
value = os.getenv(env_name)
|
||||
if not value:
|
||||
continue
|
||||
|
||||
for item in value.split(","):
|
||||
origin = normalize_origin(item)
|
||||
if origin:
|
||||
origins.append(origin)
|
||||
|
||||
return origins
|
||||
|
||||
|
||||
ENV_FILE = Path(os.getenv("DJANGO_ENV_FILE", BASE_DIR / ".env"))
|
||||
load_env_file(ENV_FILE)
|
||||
if not ENV_FILE.exists():
|
||||
@@ -70,11 +147,18 @@ SECRET_KEY = os.getenv("SECRET_KEY", "django-insecure-*%u*i415=%@ny05mbd-j3802bc
|
||||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = env_bool("DEBUG", True)
|
||||
|
||||
ALLOWED_HOSTS = env_list("ALLOWED_HOSTS", ["127.0.0.1", "preview-1.azimkin.dev"])
|
||||
ALLOWED_HOSTS = merge_unique(
|
||||
collect_hosts("COOLIFY_FQDN", "COOLIFY_URL"),
|
||||
env_list("ALLOWED_HOSTS", ["127.0.0.1", "localhost", "preview-1.azimkin.dev"]),
|
||||
)
|
||||
CSRF_TRUSTED_ORIGINS = env_list(
|
||||
"CSRF_TRUSTED_ORIGINS",
|
||||
["https://preview-1.azimkin.dev", "http://127.0.0.1", "http://localhost"],
|
||||
)
|
||||
CSRF_TRUSTED_ORIGINS = merge_unique(
|
||||
collect_origins("COOLIFY_URL", "COOLIFY_FQDN"),
|
||||
CSRF_TRUSTED_ORIGINS,
|
||||
)
|
||||
USE_X_FORWARDED_HOST = env_bool("USE_X_FORWARDED_HOST", True)
|
||||
SECURE_PROXY_SSL_HEADER = env_pair(
|
||||
"SECURE_PROXY_SSL_HEADER",
|
||||
|
||||
Reference in New Issue
Block a user