fix: Use direct env assignment in run_server.py
Changed from os.environ.setdefault() to direct os.environ[] assignment for database, API, and auth config. This ensures config.json values override any pre-existing environment variables (including empty ones from .env files). Also added debug output to help diagnose config loading issues. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -44,40 +44,40 @@ def apply_config_to_env(config: dict) -> None:
|
||||
if "port" in backend_config:
|
||||
os.environ.setdefault("BACKEND_PORT", str(backend_config["port"]))
|
||||
|
||||
# Database configuration
|
||||
# Database configuration - use direct assignment to ensure config values are used
|
||||
db_config = backend_config.get("database", {})
|
||||
if "host" in db_config:
|
||||
os.environ.setdefault("DB_HOST", db_config["host"])
|
||||
os.environ["DB_HOST"] = db_config["host"]
|
||||
if "port" in db_config:
|
||||
os.environ.setdefault("DB_PORT", str(db_config["port"]))
|
||||
os.environ["DB_PORT"] = str(db_config["port"])
|
||||
if "user" in db_config:
|
||||
os.environ.setdefault("DB_USER", db_config["user"])
|
||||
os.environ["DB_USER"] = db_config["user"]
|
||||
if "password" in db_config:
|
||||
os.environ.setdefault("DB_PASS", db_config["password"])
|
||||
os.environ["DB_PASS"] = db_config["password"]
|
||||
if "database" in db_config:
|
||||
os.environ.setdefault("DB_NAME", db_config["database"])
|
||||
os.environ["DB_NAME"] = db_config["database"]
|
||||
if "poolSize" in db_config:
|
||||
os.environ.setdefault("DB_POOL_SIZE", str(db_config["poolSize"]))
|
||||
os.environ["DB_POOL_SIZE"] = str(db_config["poolSize"])
|
||||
|
||||
# External API configuration
|
||||
# External API configuration - use direct assignment
|
||||
api_config = backend_config.get("externalApis", {})
|
||||
if "authApiUrl" in api_config:
|
||||
os.environ.setdefault("AUTH_API_URL", api_config["authApiUrl"])
|
||||
os.environ["AUTH_API_URL"] = api_config["authApiUrl"]
|
||||
if "difyApiUrl" in api_config:
|
||||
os.environ.setdefault("DIFY_API_URL", api_config["difyApiUrl"])
|
||||
os.environ["DIFY_API_URL"] = api_config["difyApiUrl"]
|
||||
if "difyApiKey" in api_config:
|
||||
os.environ.setdefault("DIFY_API_KEY", api_config["difyApiKey"])
|
||||
os.environ["DIFY_API_KEY"] = api_config["difyApiKey"]
|
||||
if "difySttApiKey" in api_config:
|
||||
os.environ.setdefault("DIFY_STT_API_KEY", api_config["difySttApiKey"])
|
||||
os.environ["DIFY_STT_API_KEY"] = api_config["difySttApiKey"]
|
||||
|
||||
# Authentication configuration
|
||||
# Authentication configuration - use direct assignment
|
||||
auth_config = backend_config.get("auth", {})
|
||||
if "adminEmail" in auth_config:
|
||||
os.environ.setdefault("ADMIN_EMAIL", auth_config["adminEmail"])
|
||||
os.environ["ADMIN_EMAIL"] = auth_config["adminEmail"]
|
||||
if "jwtSecret" in auth_config:
|
||||
os.environ.setdefault("JWT_SECRET", auth_config["jwtSecret"])
|
||||
os.environ["JWT_SECRET"] = auth_config["jwtSecret"]
|
||||
if "jwtExpireHours" in auth_config:
|
||||
os.environ.setdefault("JWT_EXPIRE_HOURS", str(auth_config["jwtExpireHours"]))
|
||||
os.environ["JWT_EXPIRE_HOURS"] = str(auth_config["jwtExpireHours"])
|
||||
|
||||
# File configuration - set TEMPLATE_DIR and RECORD_DIR relative to base
|
||||
base_dir = get_base_dir()
|
||||
@@ -116,8 +116,21 @@ def main():
|
||||
|
||||
# Load and apply configuration
|
||||
config = load_config(args.config)
|
||||
|
||||
# Debug: print loaded config
|
||||
print(f"DEBUG: Config path: {args.config}", flush=True)
|
||||
print(f"DEBUG: Loaded config keys: {list(config.keys())}", flush=True)
|
||||
backend_config = config.get("backend", {})
|
||||
db_config = backend_config.get("database", {})
|
||||
print(f"DEBUG: DB config: host={db_config.get('host')}, user={db_config.get('user')}, pass={'***' if db_config.get('password') else 'EMPTY'}", flush=True)
|
||||
|
||||
apply_config_to_env(config)
|
||||
|
||||
# Debug: print env vars after setting
|
||||
print(f"DEBUG: ENV DB_HOST={os.environ.get('DB_HOST')}", flush=True)
|
||||
print(f"DEBUG: ENV DB_USER={os.environ.get('DB_USER')}", flush=True)
|
||||
print(f"DEBUG: ENV DB_PASS={'***' if os.environ.get('DB_PASS') else 'EMPTY'}", flush=True)
|
||||
|
||||
# Command line arguments override everything
|
||||
if args.host:
|
||||
os.environ["BACKEND_HOST"] = args.host
|
||||
|
||||
Reference in New Issue
Block a user