Просмотр исходного кода

Load API CORS origins from project env

Sam Lee 3 недель назад
Родитель
Сommit
c8698bd00f
1 измененных файлов с 21 добавлено и 3 удалено
  1. 21 3
      content_agent/api.py

+ 21 - 3
content_agent/api.py

@@ -39,11 +39,29 @@ from content_agent.schemas import (
 app = FastAPI(title="Content Agent V1")
 service = RunService.from_env()
 
+def _load_project_env_value(key: str) -> str | None:
+    if os.environ.get(key) is not None:
+        return os.environ[key]
+    env_path = os.environ.get("CONTENT_AGENT_ENV_FILE") or ".env"
+    try:
+        lines = open(env_path, encoding="utf-8").read().splitlines()
+    except OSError:
+        return None
+    for line in lines:
+        stripped = line.strip()
+        if not stripped or stripped.startswith("#") or "=" not in stripped:
+            continue
+        name, value = stripped.split("=", 1)
+        if name.strip() == key:
+            return value.strip().strip('"').strip("'")
+    return None
+
+
 _cors_origins = [
     origin.strip()
-    for origin in os.environ.get(
-        "CONTENT_AGENT_WEB_CORS_ORIGINS",
-        "http://localhost:3000,http://127.0.0.1:3000,http://localhost:3010,http://127.0.0.1:3010",
+    for origin in (
+        _load_project_env_value("CONTENT_AGENT_WEB_CORS_ORIGINS")
+        or "http://localhost:3000,http://127.0.0.1:3000,http://localhost:3010,http://127.0.0.1:3010"
     ).split(",")
     if origin.strip()
 ]