Shevilll commited on
Commit
cbfc92e
·
1 Parent(s): fa13500

feat: Conditionally initialize database connection pool with error handling and raise an HTTP exception if not configured.

Browse files
Files changed (1) hide show
  1. main.py +16 -7
main.py CHANGED
@@ -39,15 +39,24 @@ def configure_conn(conn):
39
 
40
  # By using a ConnectionPool, we keep the TCP connection alive to Neon.
41
  # This eliminates the ~6-8 second TLS/SSL handshake delay on every request.
42
- db_pool = ConnectionPool(
43
- conninfo=DATABASE_URL,
44
- configure=configure_conn,
45
- min_size=1,
46
- max_size=5,
47
- timeout=30.0
48
- )
 
 
 
 
 
 
 
49
 
50
  def get_db_connection():
 
 
51
  return db_pool.connection()
52
 
53
  def extract_face_embedding(file_bytes: bytes) -> tuple[list[float], str]:
 
39
 
40
  # By using a ConnectionPool, we keep the TCP connection alive to Neon.
41
  # This eliminates the ~6-8 second TLS/SSL handshake delay on every request.
42
+ db_pool = None
43
+ if DATABASE_URL:
44
+ try:
45
+ db_pool = ConnectionPool(
46
+ conninfo=DATABASE_URL,
47
+ configure=configure_conn,
48
+ min_size=1,
49
+ max_size=5,
50
+ timeout=30.0
51
+ )
52
+ except Exception as e:
53
+ print(f"WARNING: Failed to initialize database pool: {e}")
54
+ else:
55
+ print("WARNING: DATABASE_URL is not set. Database connections will fail.")
56
 
57
  def get_db_connection():
58
+ if not db_pool:
59
+ raise HTTPException(status_code=500, detail="Database is not configured or failed to connect. Please set DATABASE_URL in Hugging Face Spaces Settings -> Variables and secrets.")
60
  return db_pool.connection()
61
 
62
  def extract_face_embedding(file_bytes: bytes) -> tuple[list[float], str]: