File size: 2,607 Bytes
9c9da24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os
from dataclasses import dataclass
from typing import List


def _as_bool(raw: str, default: bool) -> bool:
    if raw is None:
        return default
    value = raw.strip().lower()
    if value in {"1", "true", "yes", "on"}:
        return True
    if value in {"0", "false", "no", "off"}:
        return False
    return default


def _as_int(raw: str, default: int) -> int:
    if raw is None:
        return default
    try:
        parsed = int(raw)
        return parsed if parsed > 0 else default
    except ValueError:
        return default


def _as_list(raw: str) -> List[str]:
    if not raw:
        return []
    return [item.strip() for item in raw.split(",") if item.strip()]


@dataclass(frozen=True)
class Settings:
    environment: str
    is_hf_space: bool
    cors_allow_origins: List[str]
    cors_allow_credentials: bool
    trusted_hosts: List[str]
    max_active_envs: int

    @property
    def is_production(self) -> bool:
        return self.environment == "production"


_DEFAULT_DEV_ORIGINS = [
    "http://127.0.0.1:8000",
    "http://localhost:8000",
    "http://127.0.0.1:8011",
    "http://localhost:8011",
]


def load_settings() -> Settings:
    environment = os.getenv("APP_ENV", "development").strip().lower() or "development"
    space_id = os.getenv("SPACE_ID", "").strip()
    space_host = os.getenv("SPACE_HOST", "").strip().lower()
    is_hf_space = bool(space_id)

    configured_origins = _as_list(os.getenv("ALLOW_ORIGINS"))
    if configured_origins:
        cors_allow_origins = configured_origins
    elif environment == "production":
        cors_allow_origins = []
    else:
        cors_allow_origins = _DEFAULT_DEV_ORIGINS

    allow_credentials_default = environment != "production"
    cors_allow_credentials = _as_bool(os.getenv("ALLOW_CREDENTIALS"), allow_credentials_default)

    # Browsers reject wildcard origins when credentials are enabled.
    if "*" in cors_allow_origins:
        cors_allow_credentials = False

    trusted_hosts = _as_list(os.getenv("TRUSTED_HOSTS"))
    if not trusted_hosts:
        # Proxy/CDN layers may rewrite Host unexpectedly; keep permissive default
        # unless TRUSTED_HOSTS is explicitly provided.
        trusted_hosts = ["*"]

    max_active_envs = _as_int(os.getenv("MAX_ACTIVE_ENVS"), 128)

    return Settings(
        environment=environment,
        is_hf_space=is_hf_space,
        cors_allow_origins=cors_allow_origins,
        cors_allow_credentials=cors_allow_credentials,
        trusted_hosts=trusted_hosts,
        max_active_envs=max_active_envs,
    )


settings = load_settings()