Commit ·
fc36730
1
Parent(s): 014e180
Support PostgreSQL via DATABASE_URL environment variable
Browse files- app/db/database.py +16 -4
- app/main.py +26 -35
- requirements.txt +1 -0
app/db/database.py
CHANGED
|
@@ -1,12 +1,23 @@
|
|
|
|
|
| 1 |
from sqlalchemy import create_engine
|
| 2 |
from sqlalchemy.ext.declarative import declarative_base
|
| 3 |
from sqlalchemy.orm import sessionmaker
|
| 4 |
|
| 5 |
-
SQLALCHEMY_DATABASE_URL = "sqlite:///./smart_reader.db"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
engine = create_engine(
|
| 8 |
-
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
|
| 9 |
-
)
|
| 10 |
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 11 |
|
| 12 |
Base = declarative_base()
|
|
@@ -17,3 +28,4 @@ def get_db():
|
|
| 17 |
yield db
|
| 18 |
finally:
|
| 19 |
db.close()
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
from sqlalchemy import create_engine
|
| 3 |
from sqlalchemy.ext.declarative import declarative_base
|
| 4 |
from sqlalchemy.orm import sessionmaker
|
| 5 |
|
| 6 |
+
SQLALCHEMY_DATABASE_URL = os.environ.get("DATABASE_URL", "sqlite:///./smart_reader.db")
|
| 7 |
+
|
| 8 |
+
# Convert postgres:// to postgresql:// if needed for SQLAlchemy 2.0 compatibility
|
| 9 |
+
if SQLALCHEMY_DATABASE_URL.startswith("postgres://"):
|
| 10 |
+
SQLALCHEMY_DATABASE_URL = SQLALCHEMY_DATABASE_URL.replace("postgres://", "postgresql://", 1)
|
| 11 |
+
|
| 12 |
+
if SQLALCHEMY_DATABASE_URL.startswith("sqlite"):
|
| 13 |
+
engine = create_engine(
|
| 14 |
+
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
|
| 15 |
+
)
|
| 16 |
+
else:
|
| 17 |
+
engine = create_engine(
|
| 18 |
+
SQLALCHEMY_DATABASE_URL, pool_pre_ping=True
|
| 19 |
+
)
|
| 20 |
|
|
|
|
|
|
|
|
|
|
| 21 |
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 22 |
|
| 23 |
Base = declarative_base()
|
|
|
|
| 28 |
yield db
|
| 29 |
finally:
|
| 30 |
db.close()
|
| 31 |
+
|
app/main.py
CHANGED
|
@@ -126,53 +126,44 @@ def generate_ai_summary(content: str) -> str:
|
|
| 126 |
|
| 127 |
# --- Auth Endpoints ---
|
| 128 |
|
| 129 |
-
def send_verification_email(to_email: str, code: str)
|
| 130 |
# For development, just print the code.
|
| 131 |
print(f"\n{'='*40}")
|
| 132 |
print(f"VERIFICATION CODE FOR {to_email}: {code}")
|
| 133 |
print(f"{'='*40}\n")
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
| 141 |
headers = {
|
| 142 |
-
"
|
| 143 |
-
"
|
| 144 |
-
"content-type": "application/json"
|
| 145 |
}
|
| 146 |
payload = {
|
| 147 |
-
"
|
| 148 |
-
|
| 149 |
-
"email": "smartreadercommunity1@gmail.com"
|
| 150 |
-
},
|
| 151 |
-
"to": [
|
| 152 |
-
{
|
| 153 |
-
"email": to_email
|
| 154 |
-
}
|
| 155 |
-
],
|
| 156 |
"subject": "كود تفعيل حسابك - Smart Reader",
|
| 157 |
-
"
|
| 158 |
}
|
| 159 |
-
|
| 160 |
try:
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
print(f"
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
return False
|
| 172 |
except Exception as e:
|
| 173 |
-
print(f"
|
| 174 |
-
print(f"[Brevo Email Exception Traceback]:\n{traceback.format_exc()}")
|
| 175 |
-
return False
|
| 176 |
|
| 177 |
@app.post("/api/v1/auth/request-verification")
|
| 178 |
def request_verification(req: schemas.EmailVerificationRequest, db: Session = Depends(get_db)):
|
|
|
|
| 126 |
|
| 127 |
# --- Auth Endpoints ---
|
| 128 |
|
| 129 |
+
def send_verification_email(to_email: str, code: str):
|
| 130 |
# For development, just print the code.
|
| 131 |
print(f"\n{'='*40}")
|
| 132 |
print(f"VERIFICATION CODE FOR {to_email}: {code}")
|
| 133 |
print(f"{'='*40}\n")
|
| 134 |
|
| 135 |
+
resend_api_key = os.environ.get("RESEND_API_KEY", "re_QUveyqc3_MtpKbD1avSzp9Q9pq7tfnEMN")
|
| 136 |
+
|
| 137 |
+
# Send email via Resend HTTPS REST API (Port 443 - 100% reliable on Cloud)
|
| 138 |
+
import urllib.request
|
| 139 |
+
import urllib.error
|
| 140 |
+
import json
|
| 141 |
+
|
| 142 |
+
url = "https://api.resend.com/emails"
|
| 143 |
headers = {
|
| 144 |
+
"Authorization": f"Bearer {resend_api_key}",
|
| 145 |
+
"Content-Type": "application/json"
|
|
|
|
| 146 |
}
|
| 147 |
payload = {
|
| 148 |
+
"from": "Smart Reader <onboarding@resend.dev>",
|
| 149 |
+
"to": [to_email],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
"subject": "كود تفعيل حسابك - Smart Reader",
|
| 151 |
+
"html": f"<div dir='rtl' style='font-family: Arial, sans-serif; padding: 25px; background-color: #f4f4f7;'><div style='max-width: 500px; margin: 0 auto; background: #ffffff; padding: 30px; border-radius: 16px; box-shadow: 0 4px 12px rgba(0,0,0,0.05); text-align: center;'><h2 style='color: #4f46e5; margin-bottom: 10px;'>Smart Reader</h2><p style='color: #4b5563; font-size: 16px;'>كود التفعيل الخاص بحسابك هو:</p><div style='background: #eef2ff; border: 2px dashed #6366f1; border-radius: 12px; padding: 15px; margin: 20px 0;'><span style='color: #4338ca; font-size: 32px; font-weight: bold; letter-spacing: 6px;'>{code}</span></div><p style='color: #9ca3af; font-size: 14px;'>هذا الكود صالح لمدة 10 دقائق فقط.</p></div></div>"
|
| 152 |
}
|
| 153 |
+
|
| 154 |
try:
|
| 155 |
+
req = urllib.request.Request(url, data=json.dumps(payload).encode('utf-8'), headers=headers, method='POST')
|
| 156 |
+
with urllib.request.urlopen(req, timeout=10) as response:
|
| 157 |
+
res_data = response.read().decode('utf-8')
|
| 158 |
+
print(f"SUCCESS: Email sent to {to_email} via Resend. Response: {res_data}")
|
| 159 |
+
except urllib.error.HTTPError as e:
|
| 160 |
+
try:
|
| 161 |
+
err_body = e.read().decode('utf-8')
|
| 162 |
+
except Exception:
|
| 163 |
+
err_body = "<unable to read body>"
|
| 164 |
+
print(f"Resend Email HTTPError {e.code} for {to_email}: Body={err_body}")
|
|
|
|
| 165 |
except Exception as e:
|
| 166 |
+
print(f"Resend Email Error for {to_email}: {e}")
|
|
|
|
|
|
|
| 167 |
|
| 168 |
@app.post("/api/v1/auth/request-verification")
|
| 169 |
def request_verification(req: schemas.EmailVerificationRequest, db: Session = Depends(get_db)):
|
requirements.txt
CHANGED
|
@@ -13,3 +13,4 @@ python-docx==1.1.2
|
|
| 13 |
passlib==1.7.4
|
| 14 |
bcrypt==4.0.1
|
| 15 |
PyJWT==2.8.0
|
|
|
|
|
|
| 13 |
passlib==1.7.4
|
| 14 |
bcrypt==4.0.1
|
| 15 |
PyJWT==2.8.0
|
| 16 |
+
psycopg2-binary==2.9.10
|