Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

165 lignes
5.1 KiB

  1. import os
  2. import re
  3. from pathlib import Path
  4. from dotenv import load_dotenv
  5. def _load_dotenv_chain() -> None:
  6. # Load local .env first
  7. load_dotenv()
  8. # Support shell-style source directives in .env, e.g. ". /data/conf/presence/core.conf"
  9. local_env = Path(".env")
  10. if not local_env.exists():
  11. return
  12. for raw_line in local_env.read_text(encoding="utf-8").splitlines():
  13. line = raw_line.strip()
  14. if not line or line.startswith("#"):
  15. continue
  16. if line.startswith(". "):
  17. target = line[2:].strip().strip('"').strip("'")
  18. elif line.startswith("source "):
  19. target = line[7:].strip().strip('"').strip("'")
  20. else:
  21. continue
  22. if not target:
  23. continue
  24. source_path = Path(target)
  25. if not source_path.is_absolute():
  26. source_path = (local_env.parent / source_path).resolve()
  27. if source_path.exists():
  28. load_dotenv(dotenv_path=source_path, override=False)
  29. _load_dotenv_chain()
  30. _SHELL_VAR_PATTERN = re.compile(r"\$\{([A-Za-z_][A-Za-z0-9_]*)\}|\$([A-Za-z_][A-Za-z0-9_]*)")
  31. def _clean(value: str) -> str:
  32. if value is None:
  33. return ""
  34. text = str(value).strip().strip('"').strip("'")
  35. if not text:
  36. return ""
  37. # Expand ${VAR} and $VAR references using already loaded environment variables.
  38. def _replace(match: re.Match) -> str:
  39. var_name = match.group(1) or match.group(2)
  40. return os.getenv(var_name, "")
  41. return _SHELL_VAR_PATTERN.sub(_replace, text)
  42. def _has_http_scheme(value: str) -> bool:
  43. return value.startswith("http://") or value.startswith("https://")
  44. def _ensure_http_scheme(value: str, default_scheme: str = "https") -> str:
  45. value = _clean(value)
  46. if not value:
  47. return ""
  48. if _has_http_scheme(value):
  49. return value
  50. if value.startswith("/"):
  51. return value
  52. return f"{default_scheme}://{value}"
  53. def _absolute_or_join(value: str, base_url: str) -> str:
  54. value = _clean(value)
  55. if not value:
  56. return ""
  57. if _has_http_scheme(value):
  58. return value
  59. if value.startswith("/"):
  60. if base_url:
  61. return f"{base_url.rstrip('/')}{value}"
  62. return ""
  63. return _ensure_http_scheme(value)
  64. def _env_first(*names: str) -> str:
  65. for name in names:
  66. value = _clean(os.getenv(name))
  67. if value:
  68. return value
  69. return ""
  70. # Keycloak configuration (look in the .env)
  71. SECRET = _env_first("SECRET", "client_secret")
  72. KEYCLOAK_AUDIENCE = _env_first("KEYCLOAK_AUDIENCE", "keycloak_audience")
  73. _raw_keycloak_server = _env_first("KEYCLOAK_SERVER", "keycloak_server")
  74. KEYCLOAK_SERVER = _ensure_http_scheme(_raw_keycloak_server)
  75. _default_realm = _env_first("KEYCLOAK_REALM", "keycloak_realm") or "API.Server.local"
  76. _raw_keycloak_issuer = _env_first("KEYCLOAK_ISSUER", "keycloak_issuer")
  77. if _raw_keycloak_issuer and "${" not in _raw_keycloak_issuer:
  78. KEYCLOAK_ISSUER = _absolute_or_join(_raw_keycloak_issuer, KEYCLOAK_SERVER)
  79. elif KEYCLOAK_SERVER:
  80. KEYCLOAK_ISSUER = f"{KEYCLOAK_SERVER.rstrip('/')}/realms/{_default_realm}"
  81. else:
  82. KEYCLOAK_ISSUER = ""
  83. _raw_keycloak_protocol = _env_first("KEYCLOAK_PROTOCOL_ENDPOINT", "keycloak_protocol_endpoint")
  84. if _raw_keycloak_protocol and "${" not in _raw_keycloak_protocol:
  85. KEYCLOAK_PROTOCOL_ENDPOINT = _absolute_or_join(_raw_keycloak_protocol, KEYCLOAK_SERVER)
  86. elif KEYCLOAK_ISSUER:
  87. KEYCLOAK_PROTOCOL_ENDPOINT = f"{KEYCLOAK_ISSUER.rstrip('/')}/protocol/openid-connect"
  88. else:
  89. KEYCLOAK_PROTOCOL_ENDPOINT = ""
  90. _raw_jwks = _env_first("KEYCLOAK_JWKS_URL", "keycloak_jwks_url")
  91. if _raw_jwks and "${" not in _raw_jwks:
  92. KEYCLOAK_JWKS_URL = _absolute_or_join(_raw_jwks, KEYCLOAK_SERVER)
  93. elif KEYCLOAK_PROTOCOL_ENDPOINT:
  94. KEYCLOAK_JWKS_URL = f"{KEYCLOAK_PROTOCOL_ENDPOINT.rstrip('/')}/certs"
  95. else:
  96. KEYCLOAK_JWKS_URL = ""
  97. _raw_auth = _env_first("KEYCLOAK_AUTH_URL", "keycloak_auth_url")
  98. if _raw_auth and "${" not in _raw_auth:
  99. KEYCLOAK_AUTH_URL = _absolute_or_join(_raw_auth, KEYCLOAK_SERVER)
  100. elif KEYCLOAK_PROTOCOL_ENDPOINT:
  101. KEYCLOAK_AUTH_URL = f"{KEYCLOAK_PROTOCOL_ENDPOINT.rstrip('/')}/auth"
  102. else:
  103. KEYCLOAK_AUTH_URL = ""
  104. _raw_token = _env_first("KEYCLOAK_TOKEN_URL", "keycloak_token_url")
  105. if _raw_token and "${" not in _raw_token:
  106. KEYCLOAK_TOKEN_URL = _absolute_or_join(_raw_token, KEYCLOAK_SERVER)
  107. elif KEYCLOAK_PROTOCOL_ENDPOINT:
  108. KEYCLOAK_TOKEN_URL = f"{KEYCLOAK_PROTOCOL_ENDPOINT.rstrip('/')}/token"
  109. else:
  110. KEYCLOAK_TOKEN_URL = ""
  111. CORE_API_URL = os.getenv("CORE_API_URL", "http://localhost:1902")
  112. MQTT_HOST = os.getenv("MQTT_HOST", "192.168.1.101")
  113. MQTT_PORT = int(os.getenv("MQTT_PORT", "1883"))
  114. MQTT_TOPIC = os.getenv("MQTT_TOPIC", "#")
  115. MQTT_VERSION = os.getenv("MQTT_VERSION", "mqttv311")
  116. MQTT_STATUS_INTERVAL = int(os.getenv("MQTT_STATUS_INTERVAL", "30"))
  117. MQTT_STALE_AFTER = int(os.getenv("MQTT_STALE_AFTER", "30"))
  118. BLE_AI_INFER_CSV = os.getenv(
  119. "BLE_AI_INFER_CSV",
  120. "/data/service/ble-ai-localizer/data/infer/infer.csv",
  121. )
  122. BLE_AI_META_DIR = os.getenv(
  123. "BLE_AI_META_DIR",
  124. "/data/service/ble-ai-localizer/data/maps/",
  125. )
  126. BLE_AI_MAPS_DIR = os.getenv(
  127. "BLE_AI_MAPS_DIR",
  128. "/data/service/ble-ai-localizer/data/maps",
  129. )