File size: 7,968 Bytes
b2c2f23
db550a4
b2c2f23
 
db550a4
b2c2f23
db550a4
b2c2f23
db550a4
 
b2c2f23
db550a4
 
 
 
b2c2f23
bf292d9
b2c2f23
db550a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf292d9
b2c2f23
 
bf292d9
 
db550a4
 
 
 
 
b2c2f23
bf292d9
 
db550a4
b2c2f23
db550a4
 
 
 
 
 
 
 
 
 
 
 
b2c2f23
db550a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2c2f23
db550a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2c2f23
 
 
db550a4
bf292d9
b2c2f23
 
db550a4
 
 
 
 
 
 
 
 
 
bf292d9
b2c2f23
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# app/config.py
import json
import os
from functools import lru_cache
from urllib.parse import quote

# pydantic v2-first import, soft-fallback to v1
try:
    from pydantic_settings import BaseSettings, SettingsConfigDict  # v2
    from pydantic import BaseModel, Field
    IS_V2 = True
except Exception:  # v1 fallback
    from pydantic import BaseSettings as _BaseSettings, BaseModel, Field
    class BaseSettings(_BaseSettings):  # shim name
        pass
    IS_V2 = False


APPSETTINGS_PATH = os.environ.get("APPSETTINGS_JSON", "appsettings.json")

def _load_json(path: str):
    if not path or not os.path.exists(path):
        return {}
    try:
        with open(path, "r", encoding="utf-8") as f:
            return json.load(f)
    except Exception:
        return {}

def _env_or(raw_val: str | None, env_name: str | None):
    """
    If raw_val == ".env" -> fetch from env_name (or the obvious uppercase key).
    Otherwise return raw_val.
    """
    if raw_val != ".env":
        return raw_val
    key = env_name or ""
    # If an explicit env name isn't passed, try a sensible default
    if not key:
        return None
    return os.environ.get(key)

def _bool(v, default=False):
    if v is None:
        return default
    if isinstance(v, bool):
        return v
    s = str(v).strip().lower()
    if s in {"1", "true", "yes", "y", "on"}:
        return True
    if s in {"0", "false", "no", "n", "off"}:
        return False
    return default


class LocalSystemUrl(BaseModel):
    ExternalUrl: str | None = None
    IPAddress: str | None = None
    RabbitHostName: str | None = None
    RabbitPort: int | None = None
    RabbitInstanceName: str | None = None
    RabbitUserName: str | None = None
    RabbitPassword: str | None = None  # might be ".env"
    RabbitVHost: str | None = None
    UseTls: bool | None = None


class _SettingsModel(BaseModel):
    # Core (env can still override each)
    AMQP_URL: str | None = None

    # Rabbit defaults (match what you already had)
    RABBIT_INSTANCE_NAME: str = "prod"
    RABBIT_EXCHANGE_TYPE: str = "topic"
    RABBIT_ROUTING_KEY: str = ""
    RABBIT_PREFETCH: int = 1

    # TLS controls (default to strict=False only because your broker uses custom certs)
    RABBIT_TLS_VERIFY: bool = False
    RABBIT_TLS_CHECK_HOSTNAME: bool = False
    RABBIT_TLS_CA_FILE: str | None = None

    SERVICE_ID: str = "gradllm"
    USE_TLS: bool = True

    EXCHANGE_TYPES: dict[str, str] = Field(default_factory=dict)

    # appsettings.json fields that we may use to assemble AMQP_URL
    LocalSystemUrl: LocalSystemUrl | None = None
    ServiceID: str | None = None
    RabbitRoutingKey: str | None = None
    RabbitExhangeType: str | None = None  # (note the spelling in your JSON)
    UseTls: bool | None = None            # top-level dup in your JSON

    # misc passthroughs if you want them later
    RedisUrl: str | None = None
    RedisSecret: str | None = None

    # pydantic config
    if IS_V2:
        model_config = SettingsConfigDict(extra="ignore")
    else:
        class Config:
            extra = "ignore"


def _build_amqp_url_from_local(local: LocalSystemUrl, top_level_use_tls: bool | None):
    """
    Build amqp(s)://user:pass@host:port/vhost?heartbeat=30
    Uses LocalSystemUrl + `.env` indirections.
    """
    if not local or not local.RabbitHostName or not local.RabbitUserName:
        return None

    # determine TLS
    use_tls = local.UseTls
    if use_tls is None:
        use_tls = top_level_use_tls
    if use_tls is None:
        use_tls = True  # default secure

    scheme = "amqps" if use_tls else "amqp"

    # password indirection
    # If RabbitPassword is ".env", read RABBIT_PASSWORD (conventional)
    raw_pwd = local.RabbitPassword
    pwd = raw_pwd if raw_pwd and raw_pwd != ".env" else os.environ.get("RABBIT_PASSWORD")
    # fall back to standard names, just in case
    if not pwd:
        pwd = os.environ.get("RabbitPassword") or os.environ.get("RABBIT_PASS")

    user = local.RabbitUserName
    host = local.RabbitHostName
    port = local.RabbitPort or (5671 if scheme == "amqps" else 5672)
    vhost = local.RabbitVHost or "/"
    vhost_enc = quote(vhost, safe="")  # encode e.g. "/vhostuser" -> "%2Fvhostuser"

    return f"{scheme}://{user}:{pwd}@{host}:{port}/{vhost_enc}?heartbeat=30"


def _merge_env_over_json(j: dict) -> _SettingsModel:
    """
    Precedence:
      1) Environment variables (HF Secrets)
      2) appsettings.json values
      3) built defaults
    We also synthesize AMQP_URL from LocalSystemUrl if not set via env.
    """
    # Start with JSON
    model = _SettingsModel(**j)

    # Map top-level JSON keys to our fields when used
    if model.ServiceID:
        model.SERVICE_ID = model.ServiceID
    if model.RabbitRoutingKey:
        model.RABBIT_ROUTING_KEY = model.RabbitRoutingKey
    if model.RabbitExhangeType:
        model.RABBIT_EXCHANGE_TYPE = model.RabbitExhangeType
    if model.UseTls is not None:
        model.USE_TLS = _bool(model.UseTls, model.USE_TLS)

    # If AMQP_URL not set, try to build from LocalSystemUrl
    if not model.AMQP_URL and model.LocalSystemUrl:
        built = _build_amqp_url_from_local(model.LocalSystemUrl, model.UseTls)
        if built:
            model.AMQP_URL = built

    # Now overlay environment variables (HF Secrets)
    env = os.environ

    # Direct env override of AMQP_URL wins
    model.AMQP_URL = env.get("AMQP_URL", model.AMQP_URL)

    # Other rabbit knobs
    model.RABBIT_INSTANCE_NAME = env.get("RABBIT_INSTANCE_NAME", model.RABBIT_INSTANCE_NAME)
    model.RABBIT_EXCHANGE_TYPE = env.get("RABBIT_EXCHANGE_TYPE", model.RABBIT_EXCHANGE_TYPE)
    model.RABBIT_ROUTING_KEY = env.get("RABBIT_ROUTING_KEY", model.RABBIT_ROUTING_KEY)
    model.RABBIT_PREFETCH = int(env.get("RABBIT_PREFETCH", model.RABBIT_PREFETCH))

    # TLS env overrides
    if "RABBIT_TLS_VERIFY" in env:
        model.RABBIT_TLS_VERIFY = _bool(env["RABBIT_TLS_VERIFY"], model.RABBIT_TLS_VERIFY)
    if "RABBIT_TLS_CHECK_HOSTNAME" in env:
        model.RABBIT_TLS_CHECK_HOSTNAME = _bool(env["RABBIT_TLS_CHECK_HOSTNAME"], model.RABBIT_TLS_CHECK_HOSTNAME)
    model.RABBIT_TLS_CA_FILE = env.get("RABBIT_TLS_CA_FILE", model.RABBIT_TLS_CA_FILE)

    # SERVICE_ID can be overridden (you renamed yours to gradllm)
    model.SERVICE_ID = env.get("SERVICE_ID", model.SERVICE_ID)

    # Optional EXCHANGE_TYPES as JSON string in env
    et = env.get("EXCHANGE_TYPES")
    if et:
        try:
            model.EXCHANGE_TYPES = json.loads(et)
        except Exception:
            pass

    # Final sanity: must have AMQP_URL
    if not model.AMQP_URL:
        raise RuntimeError(
            "AMQP_URL is not configured. Set it via:\n"
            "- env secret AMQP_URL, or\n"
            "- appsettings.json LocalSystemUrl (RabbitHostName/UserName/Password/VHost/UseTls)."
        )

    return model


class Settings(BaseSettings):
    """
    Thin wrapper that exposes the merged model as attributes.
    """
    # required
    AMQP_URL: str

    # rabbit
    RABBIT_INSTANCE_NAME: str = "prod"
    RABBIT_EXCHANGE_TYPE: str = "topic"
    RABBIT_ROUTING_KEY: str = ""
    RABBIT_PREFETCH: int = 1

    # TLS
    RABBIT_TLS_VERIFY: bool = False
    RABBIT_TLS_CHECK_HOSTNAME: bool = False
    RABBIT_TLS_CA_FILE: str | None = None

    SERVICE_ID: str = "gradllm"
    USE_TLS: bool = True

    EXCHANGE_TYPES: dict[str, str] = Field(default_factory=dict)

    if IS_V2:
        model_config = SettingsConfigDict(case_sensitive=True)
    else:
        class Config:
            case_sensitive = True


@lru_cache
def get_settings() -> Settings:
    # 1) load json
    j = _load_json(APPSETTINGS_PATH)

    # 2) merge with env precedence + synthesize AMQP_URL if needed
    merged = _merge_env_over_json(j)

    # 3) project into the public Settings class
    data = merged.model_dump() if hasattr(merged, "model_dump") else merged.dict()
    return Settings(**data)


settings = get_settings()