pick up env vars for config (not just existing .env files)
This commit is contained in:
parent
7b435a8367
commit
9e4502d07b
1 changed files with 64 additions and 51 deletions
|
|
@ -116,21 +116,8 @@ class EnvManager:
|
||||||
return f"'{escaped_value}'"
|
return f"'{escaped_value}'"
|
||||||
|
|
||||||
def load_existing_env(self) -> bool:
|
def load_existing_env(self) -> bool:
|
||||||
"""Load existing .env file if it exists."""
|
"""Load existing .env file if it exists, or fall back to environment variables."""
|
||||||
if not self.env_file.exists():
|
import os
|
||||||
return False
|
|
||||||
|
|
||||||
try:
|
|
||||||
with open(self.env_file, "r") as f:
|
|
||||||
for line in f:
|
|
||||||
line = line.strip()
|
|
||||||
if not line or line.startswith("#"):
|
|
||||||
continue
|
|
||||||
|
|
||||||
if "=" in line:
|
|
||||||
key, value = line.split("=", 1)
|
|
||||||
key = key.strip()
|
|
||||||
value = sanitize_env_value(value)
|
|
||||||
|
|
||||||
# Map env vars to config attributes
|
# Map env vars to config attributes
|
||||||
attr_map = {
|
attr_map = {
|
||||||
|
|
@ -164,14 +151,40 @@ class EnvManager:
|
||||||
"DISABLE_INGEST_WITH_LANGFLOW": "disable_ingest_with_langflow",
|
"DISABLE_INGEST_WITH_LANGFLOW": "disable_ingest_with_langflow",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
loaded_from_file = False
|
||||||
|
|
||||||
|
# Try to load from .env file first
|
||||||
|
if self.env_file.exists():
|
||||||
|
try:
|
||||||
|
with open(self.env_file, "r") as f:
|
||||||
|
for line in f:
|
||||||
|
line = line.strip()
|
||||||
|
if not line or line.startswith("#"):
|
||||||
|
continue
|
||||||
|
|
||||||
|
if "=" in line:
|
||||||
|
key, value = line.split("=", 1)
|
||||||
|
key = key.strip()
|
||||||
|
value = sanitize_env_value(value)
|
||||||
|
|
||||||
if key in attr_map:
|
if key in attr_map:
|
||||||
setattr(self.config, attr_map[key], value)
|
setattr(self.config, attr_map[key], value)
|
||||||
|
|
||||||
return True
|
loaded_from_file = True
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error("Error loading .env file", error=str(e))
|
logger.error("Error loading .env file", error=str(e))
|
||||||
return False
|
|
||||||
|
# Fall back to environment variables if .env file doesn't exist or failed to load
|
||||||
|
if not loaded_from_file:
|
||||||
|
logger.info("No .env file found, loading from environment variables")
|
||||||
|
for env_key, attr_name in attr_map.items():
|
||||||
|
value = os.environ.get(env_key, "")
|
||||||
|
if value:
|
||||||
|
setattr(self.config, attr_name, value)
|
||||||
|
return True
|
||||||
|
|
||||||
|
return loaded_from_file
|
||||||
|
|
||||||
def setup_secure_defaults(self) -> None:
|
def setup_secure_defaults(self) -> None:
|
||||||
"""Set up secure default values for passwords and keys."""
|
"""Set up secure default values for passwords and keys."""
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue