Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
@@ -21,16 +21,11 @@ except ImportError:
|
|
21 |
|
22 |
# Define environment variables for PostgreSQL connection
|
23 |
# These should be set in the environment where you run this script
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
DB_USER = "postgres"
|
30 |
-
#DB_PASSWORD = os.getenv("DB_PASSWORD")
|
31 |
-
DB_PASSWORD = "Me21322972.........."
|
32 |
-
#DB_PORT = os.getenv("DB_PORT", "5432") # Default PostgreSQL port
|
33 |
-
DB_PORT = "5432"
|
34 |
|
35 |
# Define environment variables for Google Sheets authentication
|
36 |
GOOGLE_BASE64_CREDENTIALS = os.getenv("GOOGLE_BASE64_CREDENTIALS")
|
@@ -47,16 +42,32 @@ EMBEDDING_DIM = 384 # Dimension for paraphrase-MiniLM-L6-v2
|
|
47 |
def connect_db():
|
48 |
"""Establishes a connection to the PostgreSQL database."""
|
49 |
print("Attempting to connect to the database...")
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
print("Error: Database credentials (DB_HOST, DB_NAME, DB_USER, DB_PASSWORD) are not fully set as environment variables.")
|
52 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
try:
|
54 |
conn = psycopg2.connect(
|
55 |
-
host=
|
56 |
-
database=
|
57 |
-
user=
|
58 |
-
password=
|
59 |
-
port=
|
60 |
)
|
61 |
print("Database connection successful.")
|
62 |
return conn
|
|
|
21 |
|
22 |
# Define environment variables for PostgreSQL connection
|
23 |
# These should be set in the environment where you run this script
|
24 |
+
DB_HOST = os.getenv("DB_HOST")
|
25 |
+
DB_NAME = os.getenv("DB_NAME")
|
26 |
+
DB_USER = os.getenv("DB_USER")
|
27 |
+
DB_PASSWORD = os.getenv("DB_PASSWORD")
|
28 |
+
DB_PORT = os.getenv("DB_PORT", "5432") # Default PostgreSQL port
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
# Define environment variables for Google Sheets authentication
|
31 |
GOOGLE_BASE64_CREDENTIALS = os.getenv("GOOGLE_BASE64_CREDENTIALS")
|
|
|
42 |
def connect_db():
|
43 |
"""Establishes a connection to the PostgreSQL database."""
|
44 |
print("Attempting to connect to the database...")
|
45 |
+
# Retrieve credentials inside the function in case environment variables are set after import
|
46 |
+
db_host = os.getenv("DB_HOST")
|
47 |
+
db_name = os.getenv("DB_NAME")
|
48 |
+
db_user = os.getenv("DB_USER")
|
49 |
+
db_password = os.getenv("DB_PASSWORD")
|
50 |
+
db_port = os.getenv("DB_PORT", "5432") # Default PostgreSQL port
|
51 |
+
|
52 |
+
|
53 |
+
if not all([db_host, db_name, db_user, db_password]):
|
54 |
print("Error: Database credentials (DB_HOST, DB_NAME, DB_USER, DB_PASSWORD) are not fully set as environment variables.")
|
55 |
return None
|
56 |
+
|
57 |
+
# *** FIX: Remove http(s):// prefix from host if present ***
|
58 |
+
if db_host.startswith("https://"):
|
59 |
+
db_host = db_host.replace("https://", "")
|
60 |
+
elif db_host.startswith("http://"):
|
61 |
+
db_host = db_host.replace("http://", "")
|
62 |
+
# **********************************************************
|
63 |
+
|
64 |
try:
|
65 |
conn = psycopg2.connect(
|
66 |
+
host=db_host,
|
67 |
+
database=db_name,
|
68 |
+
user=db_user,
|
69 |
+
password=db_password,
|
70 |
+
port=db_port
|
71 |
)
|
72 |
print("Database connection successful.")
|
73 |
return conn
|