Commit
·
c5e43db
1
Parent(s):
096cd9b
config.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
# app/config.py
|
|
|
|
|
|
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
from functools import lru_cache
|
|
@@ -35,7 +37,6 @@ def _env_or(raw_val: str | None, env_name: str | None):
|
|
| 35 |
if raw_val != ".env":
|
| 36 |
return raw_val
|
| 37 |
key = env_name or ""
|
| 38 |
-
# If an explicit env name isn't passed, try a sensible default
|
| 39 |
if not key:
|
| 40 |
return None
|
| 41 |
return os.environ.get(key)
|
|
@@ -69,7 +70,7 @@ class _SettingsModel(BaseModel):
|
|
| 69 |
# Core (env can still override each)
|
| 70 |
AMQP_URL: str | None = None
|
| 71 |
|
| 72 |
-
# Rabbit defaults
|
| 73 |
RABBIT_INSTANCE_NAME: str = "prod"
|
| 74 |
RABBIT_EXCHANGE_TYPE: str = "topic"
|
| 75 |
RABBIT_ROUTING_KEY: str = ""
|
|
@@ -86,7 +87,8 @@ class _SettingsModel(BaseModel):
|
|
| 86 |
EXCHANGE_TYPES: dict[str, str] = Field(default_factory=dict)
|
| 87 |
|
| 88 |
# appsettings.json fields that we may use to assemble AMQP_URL
|
| 89 |
-
|
|
|
|
| 90 |
ServiceID: str | None = None
|
| 91 |
RabbitRoutingKey: str | None = None
|
| 92 |
RabbitExhangeType: str | None = None # (note the spelling in your JSON)
|
|
@@ -96,12 +98,12 @@ class _SettingsModel(BaseModel):
|
|
| 96 |
RedisUrl: str | None = None
|
| 97 |
RedisSecret: str | None = None
|
| 98 |
|
| 99 |
-
# pydantic config
|
| 100 |
if IS_V2:
|
| 101 |
-
model_config = SettingsConfigDict(extra="ignore")
|
| 102 |
else:
|
| 103 |
class Config:
|
| 104 |
extra = "ignore"
|
|
|
|
| 105 |
|
| 106 |
|
| 107 |
def _build_amqp_url_from_local(local: LocalSystemUrl, top_level_use_tls: bool | None):
|
|
@@ -122,10 +124,8 @@ def _build_amqp_url_from_local(local: LocalSystemUrl, top_level_use_tls: bool |
|
|
| 122 |
scheme = "amqps" if use_tls else "amqp"
|
| 123 |
|
| 124 |
# password indirection
|
| 125 |
-
# If RabbitPassword is ".env", read RABBIT_PASSWORD (conventional)
|
| 126 |
raw_pwd = local.RabbitPassword
|
| 127 |
pwd = raw_pwd if raw_pwd and raw_pwd != ".env" else os.environ.get("RABBIT_PASSWORD")
|
| 128 |
-
# fall back to standard names, just in case
|
| 129 |
if not pwd:
|
| 130 |
pwd = os.environ.get("RabbitPassword") or os.environ.get("RABBIT_PASS")
|
| 131 |
|
|
@@ -133,7 +133,7 @@ def _build_amqp_url_from_local(local: LocalSystemUrl, top_level_use_tls: bool |
|
|
| 133 |
host = local.RabbitHostName
|
| 134 |
port = local.RabbitPort or (5671 if scheme == "amqps" else 5672)
|
| 135 |
vhost = local.RabbitVHost or "/"
|
| 136 |
-
vhost_enc = quote(vhost, safe="")
|
| 137 |
|
| 138 |
return f"{scheme}://{user}:{pwd}@{host}:{port}/{vhost_enc}?heartbeat=30"
|
| 139 |
|
|
@@ -159,9 +159,9 @@ def _merge_env_over_json(j: dict) -> _SettingsModel:
|
|
| 159 |
if model.UseTls is not None:
|
| 160 |
model.USE_TLS = _bool(model.UseTls, model.USE_TLS)
|
| 161 |
|
| 162 |
-
# If AMQP_URL not set, try to build from LocalSystemUrl
|
| 163 |
-
if not model.AMQP_URL and model.
|
| 164 |
-
built = _build_amqp_url_from_local(model.
|
| 165 |
if built:
|
| 166 |
model.AMQP_URL = built
|
| 167 |
|
|
@@ -184,7 +184,7 @@ def _merge_env_over_json(j: dict) -> _SettingsModel:
|
|
| 184 |
model.RABBIT_TLS_CHECK_HOSTNAME = _bool(env["RABBIT_TLS_CHECK_HOSTNAME"], model.RABBIT_TLS_CHECK_HOSTNAME)
|
| 185 |
model.RABBIT_TLS_CA_FILE = env.get("RABBIT_TLS_CA_FILE", model.RABBIT_TLS_CA_FILE)
|
| 186 |
|
| 187 |
-
# SERVICE_ID can be overridden
|
| 188 |
model.SERVICE_ID = env.get("SERVICE_ID", model.SERVICE_ID)
|
| 189 |
|
| 190 |
# Optional EXCHANGE_TYPES as JSON string in env
|
|
|
|
| 1 |
# app/config.py
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
import json
|
| 5 |
import os
|
| 6 |
from functools import lru_cache
|
|
|
|
| 37 |
if raw_val != ".env":
|
| 38 |
return raw_val
|
| 39 |
key = env_name or ""
|
|
|
|
| 40 |
if not key:
|
| 41 |
return None
|
| 42 |
return os.environ.get(key)
|
|
|
|
| 70 |
# Core (env can still override each)
|
| 71 |
AMQP_URL: str | None = None
|
| 72 |
|
| 73 |
+
# Rabbit defaults
|
| 74 |
RABBIT_INSTANCE_NAME: str = "prod"
|
| 75 |
RABBIT_EXCHANGE_TYPE: str = "topic"
|
| 76 |
RABBIT_ROUTING_KEY: str = ""
|
|
|
|
| 87 |
EXCHANGE_TYPES: dict[str, str] = Field(default_factory=dict)
|
| 88 |
|
| 89 |
# appsettings.json fields that we may use to assemble AMQP_URL
|
| 90 |
+
# Rename the field to avoid clashing with the type name; keep alias for input
|
| 91 |
+
LocalSystem: LocalSystemUrl | None = Field(default=None, alias="LocalSystemUrl")
|
| 92 |
ServiceID: str | None = None
|
| 93 |
RabbitRoutingKey: str | None = None
|
| 94 |
RabbitExhangeType: str | None = None # (note the spelling in your JSON)
|
|
|
|
| 98 |
RedisUrl: str | None = None
|
| 99 |
RedisSecret: str | None = None
|
| 100 |
|
|
|
|
| 101 |
if IS_V2:
|
| 102 |
+
model_config = SettingsConfigDict(extra="ignore", populate_by_name=True)
|
| 103 |
else:
|
| 104 |
class Config:
|
| 105 |
extra = "ignore"
|
| 106 |
+
allow_population_by_field_name = True
|
| 107 |
|
| 108 |
|
| 109 |
def _build_amqp_url_from_local(local: LocalSystemUrl, top_level_use_tls: bool | None):
|
|
|
|
| 124 |
scheme = "amqps" if use_tls else "amqp"
|
| 125 |
|
| 126 |
# password indirection
|
|
|
|
| 127 |
raw_pwd = local.RabbitPassword
|
| 128 |
pwd = raw_pwd if raw_pwd and raw_pwd != ".env" else os.environ.get("RABBIT_PASSWORD")
|
|
|
|
| 129 |
if not pwd:
|
| 130 |
pwd = os.environ.get("RabbitPassword") or os.environ.get("RABBIT_PASS")
|
| 131 |
|
|
|
|
| 133 |
host = local.RabbitHostName
|
| 134 |
port = local.RabbitPort or (5671 if scheme == "amqps" else 5672)
|
| 135 |
vhost = local.RabbitVHost or "/"
|
| 136 |
+
vhost_enc = quote(vhost, safe="")
|
| 137 |
|
| 138 |
return f"{scheme}://{user}:{pwd}@{host}:{port}/{vhost_enc}?heartbeat=30"
|
| 139 |
|
|
|
|
| 159 |
if model.UseTls is not None:
|
| 160 |
model.USE_TLS = _bool(model.UseTls, model.USE_TLS)
|
| 161 |
|
| 162 |
+
# If AMQP_URL not set, try to build from LocalSystem (aliased from LocalSystemUrl)
|
| 163 |
+
if not model.AMQP_URL and model.LocalSystem:
|
| 164 |
+
built = _build_amqp_url_from_local(model.LocalSystem, model.UseTls)
|
| 165 |
if built:
|
| 166 |
model.AMQP_URL = built
|
| 167 |
|
|
|
|
| 184 |
model.RABBIT_TLS_CHECK_HOSTNAME = _bool(env["RABBIT_TLS_CHECK_HOSTNAME"], model.RABBIT_TLS_CHECK_HOSTNAME)
|
| 185 |
model.RABBIT_TLS_CA_FILE = env.get("RABBIT_TLS_CA_FILE", model.RABBIT_TLS_CA_FILE)
|
| 186 |
|
| 187 |
+
# SERVICE_ID can be overridden
|
| 188 |
model.SERVICE_ID = env.get("SERVICE_ID", model.SERVICE_ID)
|
| 189 |
|
| 190 |
# Optional EXCHANGE_TYPES as JSON string in env
|