Dashm commited on
Commit
0448b2b
·
1 Parent(s): ea3f6ff

Fix: prefer IPv4 via gai.conf, revert database.py to simple connection

Browse files
Files changed (2) hide show
  1. Dockerfile +2 -1
  2. backend/database.py +7 -33
Dockerfile CHANGED
@@ -5,7 +5,8 @@ WORKDIR /app
5
  # Install system dependencies
6
  RUN apt-get update && apt-get install -y \
7
  build-essential \
8
- && rm -rf /var/lib/apt/lists/*
 
9
 
10
  # Install CPU-only PyTorch first (prevents pip from pulling GPU version)
11
  RUN pip install --no-cache-dir torch --index-url https://download.pytorch.org/whl/cpu
 
5
  # Install system dependencies
6
  RUN apt-get update && apt-get install -y \
7
  build-essential \
8
+ && rm -rf /var/lib/apt/lists/* \
9
+ && echo 'precedence ::ffff:0:0/96 100' >> /etc/gai.conf
10
 
11
  # Install CPU-only PyTorch first (prevents pip from pulling GPU version)
12
  RUN pip install --no-cache-dir torch --index-url https://download.pytorch.org/whl/cpu
backend/database.py CHANGED
@@ -7,9 +7,9 @@ and supabase-py for standard CRUD operations.
7
  import os
8
  import psycopg
9
  from psycopg.rows import dict_row
10
- from urllib.parse import urlparse
11
  import numpy as np
12
  from supabase import create_client, Client
 
13
 
14
  # --- Supabase Client (for CRUD operations) ---
15
 
@@ -20,44 +20,18 @@ def get_supabase() -> Client:
20
  """Get or create the Supabase client for standard CRUD operations."""
21
  global _supabase_client
22
  if _supabase_client is None:
23
- _supabase_client = create_client(
24
- os.environ["SUPABASE_URL"].strip(),
25
- os.environ["SUPABASE_KEY"].strip(),
26
- )
27
  return _supabase_client
28
 
29
 
30
  # --- Direct PostgreSQL connection (for pgvector queries) ---
31
 
32
- _cached_ipv4 = None
33
-
34
  def get_db_connection():
35
- """Create a new psycopg3 connection, forcing IPv4 via hostaddr."""
36
- import socket
37
- global _cached_ipv4
38
- db_url = os.environ["DATABASE_URL"].strip()
39
- parsed = urlparse(db_url)
40
- hostname = parsed.hostname
41
-
42
- # Resolve hostname to IPv4 once and cache it
43
- if _cached_ipv4 is None:
44
- results = socket.getaddrinfo(hostname, parsed.port or 5432, socket.AF_INET, socket.SOCK_STREAM)
45
- if not results:
46
- raise RuntimeError(f"Cannot resolve {hostname} to IPv4")
47
- _cached_ipv4 = results[0][4][0]
48
- print(f"[DB] Resolved {hostname} -> {_cached_ipv4} (IPv4)")
49
-
50
- # Use conninfo string with hostaddr to bypass libpq DNS
51
- conninfo = (
52
- f"host={hostname} "
53
- f"hostaddr={_cached_ipv4} "
54
- f"port={parsed.port or 5432} "
55
- f"user={parsed.username} "
56
- f"password={parsed.password} "
57
- f"dbname={parsed.path.lstrip('/')} "
58
- f"sslmode=require"
59
- )
60
- conn = psycopg.connect(conninfo, row_factory=dict_row)
61
  return conn
62
 
63
 
 
7
  import os
8
  import psycopg
9
  from psycopg.rows import dict_row
 
10
  import numpy as np
11
  from supabase import create_client, Client
12
+ from config import SUPABASE_URL, SUPABASE_KEY, DATABASE_URL
13
 
14
  # --- Supabase Client (for CRUD operations) ---
15
 
 
20
  """Get or create the Supabase client for standard CRUD operations."""
21
  global _supabase_client
22
  if _supabase_client is None:
23
+ url = (os.environ.get("SUPABASE_URL") or SUPABASE_URL).strip()
24
+ key = (os.environ.get("SUPABASE_KEY") or SUPABASE_KEY).strip()
25
+ _supabase_client = create_client(url, key)
 
26
  return _supabase_client
27
 
28
 
29
  # --- Direct PostgreSQL connection (for pgvector queries) ---
30
 
 
 
31
  def get_db_connection():
32
+ """Create a new psycopg3 connection for pgvector queries."""
33
+ db_url = (os.environ.get("DATABASE_URL") or DATABASE_URL).strip()
34
+ conn = psycopg.connect(db_url, row_factory=dict_row)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  return conn
36
 
37