|
8 | 8 |
|
9 | 9 | from app.utils.config_utils import get_config_bool, get_config_value |
10 | 10 | from app.utils.types import OutputMode |
11 | | -from app.utils.vault_client import VaultClient, get_secret_or_env |
| 11 | +from app.utils.vault_client import get_secret_or_env |
12 | 12 |
|
13 | | -_vault = VaultClient() |
14 | | - |
15 | | -# @lru_cache |
16 | | -# def get_config_value(key: str, default: Optional[str] = None) -> str: |
17 | | -# """ |
18 | | -# Retrieve a configuration value from Vault, environment variable, or fallback. |
19 | | - |
20 | | -# Args: |
21 | | -# key (str): The configuration key to retrieve. |
22 | | -# default (Optional[str]): Fallback value if the key is not found. |
23 | | - |
24 | | -# Returns: |
25 | | -# str: The resolved configuration value. |
26 | | - |
27 | | -# Raises: |
28 | | -# ValueError: If no value is found and no default is provided. |
29 | | -# """ |
30 | | -# val = _vault.get(key, os.getenv(key)) |
31 | | -# if val is None: |
32 | | -# if default is not None: |
33 | | -# return str(default) |
34 | | -# raise ValueError(f"❌ Missing required config for key: {key}") |
35 | | -# return str(val) |
36 | | - |
37 | | - |
38 | | -# @lru_cache |
39 | | -# def get_config_bool(key: str, default: bool = False) -> bool: |
40 | | -# """ |
41 | | -# Retrieve a boolean configuration value with support for multiple true-like values. |
42 | | - |
43 | | -# Args: |
44 | | -# key (str): The configuration key to retrieve. |
45 | | -# default (bool): Default value if the key is not found. |
46 | | - |
47 | | -# Returns: |
48 | | -# bool: The resolved boolean configuration value. |
49 | | -# """ |
50 | | -# val = get_config_value(key, str(default)).strip().lower() |
51 | | -# return val in {"1", "true", "yes", "on"} |
| 13 | +# _vault = VaultClient() |
52 | 14 |
|
53 | 15 |
|
54 | 16 | @lru_cache |
@@ -1371,3 +1333,136 @@ def get_healthcheck_port() -> int: |
1371 | 1333 |
|
1372 | 1334 | """ |
1373 | 1335 | return int(get_config_value("HEALTHCHECK_PORT", "8081")) |
| 1336 | + |
| 1337 | + |
| 1338 | +@lru_cache |
| 1339 | +def get_metrics_enabled() -> bool: |
| 1340 | + """Determine whether the Prometheus metrics server should be started. |
| 1341 | +
|
| 1342 | + This checks the value of METRICS_ENABLED from Vault or the environment. |
| 1343 | + Accepts truthy values: "1", "true", "yes" (case-insensitive). |
| 1344 | +
|
| 1345 | + Returns: |
| 1346 | + bool: True if metrics are enabled, otherwise False. |
| 1347 | +
|
| 1348 | + """ |
| 1349 | + val = get_config_value("METRICS_ENABLED", "true").lower() |
| 1350 | + return val in ("1", "true", "yes") |
| 1351 | + |
| 1352 | + |
| 1353 | +@lru_cache |
| 1354 | +def get_metrics_port() -> int: |
| 1355 | + """Retrieve the TCP port number on which the Prometheus metrics server should listen. |
| 1356 | +
|
| 1357 | + Returns: |
| 1358 | + int: Port number (default: 8000). |
| 1359 | +
|
| 1360 | + Raises: |
| 1361 | + ValueError: If METRICS_PORT is not a valid integer. |
| 1362 | +
|
| 1363 | + """ |
| 1364 | + port_str = get_config_value("METRICS_PORT", "8000") |
| 1365 | + try: |
| 1366 | + return int(port_str) |
| 1367 | + except ValueError: |
| 1368 | + raise ValueError(f"❌ Invalid METRICS_PORT value: '{port_str}' must be an integer.") |
| 1369 | + |
| 1370 | + |
| 1371 | +@lru_cache |
| 1372 | +def get_metrics_bind_address() -> str: |
| 1373 | + """Get the network address to bind the metrics server. |
| 1374 | +
|
| 1375 | + Returns: |
| 1376 | + str: Bind address (default: "0.0.0.0"). |
| 1377 | +
|
| 1378 | + """ |
| 1379 | + return get_config_value("METRICS_BIND_ADDRESS", "0.0.0.0") |
| 1380 | + |
| 1381 | + |
| 1382 | +@lru_cache |
| 1383 | +def get_config_bool(key: str, default: bool = False) -> bool: |
| 1384 | + """Retrieve a boolean configuration value from Vault or environment. |
| 1385 | +
|
| 1386 | + Interprets common truthy values ("1", "true", "yes", "on") as True. |
| 1387 | + Uses a string fallback if the key is not found. |
| 1388 | +
|
| 1389 | + Args: |
| 1390 | + key (str): The configuration key to retrieve. |
| 1391 | + default (bool): The fallback value if key is missing or invalid. |
| 1392 | +
|
| 1393 | + Returns: |
| 1394 | + bool: Boolean value for the requested configuration key. |
| 1395 | +
|
| 1396 | + """ |
| 1397 | + val: str = get_config_value(key, str(default)).lower() |
| 1398 | + return val in ("1", "true", "yes", "on") |
| 1399 | + |
| 1400 | + |
| 1401 | +@lru_cache |
| 1402 | +def get_output_modes() -> List[str]: |
| 1403 | + """Retrieve a list of enabled output modes. |
| 1404 | +
|
| 1405 | + This reads the OUTPUT_MODES config key, splits it by commas, and returns |
| 1406 | + a lowercase list of modes like "s3", "rest", or "database". |
| 1407 | +
|
| 1408 | + Returns: |
| 1409 | + List[str]: Enabled output modes. |
| 1410 | +
|
| 1411 | + """ |
| 1412 | + modes: str = get_config_value("OUTPUT_MODES", "") |
| 1413 | + return [m.strip().lower() for m in modes.split(",") if m.strip()] |
| 1414 | + |
| 1415 | + |
| 1416 | +@lru_cache |
| 1417 | +def get_rest_output_url() -> str: |
| 1418 | + """Get the REST endpoint URL for output dispatch. |
| 1419 | +
|
| 1420 | + Returns: |
| 1421 | + str: Fully qualified REST API URL. |
| 1422 | +
|
| 1423 | + """ |
| 1424 | + return get_config_value("REST_OUTPUT_URL") |
| 1425 | + |
| 1426 | + |
| 1427 | +@lru_cache |
| 1428 | +def get_s3_output_bucket() -> str: |
| 1429 | + """Get the name of the S3 bucket used for output dispatch. |
| 1430 | +
|
| 1431 | + Returns: |
| 1432 | + str: S3 bucket name. |
| 1433 | +
|
| 1434 | + """ |
| 1435 | + return get_config_value("S3_OUTPUT_BUCKET") |
| 1436 | + |
| 1437 | + |
| 1438 | +@lru_cache |
| 1439 | +def get_s3_output_prefix() -> str: |
| 1440 | + """Get the object key prefix used for S3 output files. |
| 1441 | +
|
| 1442 | + Returns: |
| 1443 | + str: S3 key prefix (can be empty). |
| 1444 | +
|
| 1445 | + """ |
| 1446 | + return get_config_value("S3_OUTPUT_PREFIX", "") |
| 1447 | + |
| 1448 | + |
| 1449 | +@lru_cache |
| 1450 | +def get_database_output_url() -> str: |
| 1451 | + """Get the SQLAlchemy-compatible connection string for output database. |
| 1452 | +
|
| 1453 | + Returns: |
| 1454 | + str: Database URL (e.g., postgresql://user:pass@host/db). |
| 1455 | +
|
| 1456 | + """ |
| 1457 | + return get_config_value("DATABASE_OUTPUT_URL") |
| 1458 | + |
| 1459 | + |
| 1460 | +@lru_cache |
| 1461 | +def get_database_insert_sql() -> str: |
| 1462 | + """Get the raw SQL INSERT statement template for database output. |
| 1463 | +
|
| 1464 | + Returns: |
| 1465 | + str: SQL insert statement. |
| 1466 | +
|
| 1467 | + """ |
| 1468 | + return get_config_value("DATABASE_INSERT_SQL") |
0 commit comments