norhan12 commited on
Commit
3808a22
·
verified ·
1 Parent(s): ba42f3b

Tolerate copy-paste mistakes in DATABASE_URL

Browse files
Files changed (1) hide show
  1. platform_app/core.py +17 -1
platform_app/core.py CHANGED
@@ -2,6 +2,7 @@
2
  import json
3
  import logging
4
  import os
 
5
  import threading
6
  import time
7
  from typing import Any, Dict, List, Optional
@@ -14,7 +15,22 @@ logger = logging.getLogger("evalbot.platform")
14
  # --------------------------------------------------------------------------------------
15
  # Settings (all from environment / Space secrets)
16
  # --------------------------------------------------------------------------------------
17
- DATABASE_URL = os.getenv("DATABASE_URL", "")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  SUPABASE_URL = os.getenv("SUPABASE_URL", "").rstrip("/")
19
  SUPABASE_ANON_KEY = os.getenv("SUPABASE_ANON_KEY", "")
20
  SUPABASE_SERVICE_ROLE_KEY = os.getenv("SUPABASE_SERVICE_ROLE_KEY", "")
 
2
  import json
3
  import logging
4
  import os
5
+ import re
6
  import threading
7
  import time
8
  from typing import Any, Dict, List, Optional
 
15
  # --------------------------------------------------------------------------------------
16
  # Settings (all from environment / Space secrets)
17
  # --------------------------------------------------------------------------------------
18
+ def _normalize_db_url(raw: str) -> str:
19
+ """Forgive common copy-paste mistakes in the DATABASE_URL secret: stray spaces or newlines,
20
+ the [ ] placeholder brackets left around the password, and unescaped symbols in the password."""
21
+ from urllib.parse import quote, unquote
22
+
23
+ url = re.sub(r"\s+", "", raw or "")
24
+ m = re.match(r"^(postgres(?:ql)?://)([^:/@]+):(.*)@([^@]+)$", url)
25
+ if not m:
26
+ return url
27
+ scheme, user, password, rest = m.groups()
28
+ if password.startswith("[") and password.endswith("]"):
29
+ password = password[1:-1]
30
+ return f"{scheme}{user}:{quote(unquote(password), safe='')}@{rest}"
31
+
32
+
33
+ DATABASE_URL = _normalize_db_url(os.getenv("DATABASE_URL", ""))
34
  SUPABASE_URL = os.getenv("SUPABASE_URL", "").rstrip("/")
35
  SUPABASE_ANON_KEY = os.getenv("SUPABASE_ANON_KEY", "")
36
  SUPABASE_SERVICE_ROLE_KEY = os.getenv("SUPABASE_SERVICE_ROLE_KEY", "")