Spaces:
Runtime error
Runtime error
Kushal commited on
Commit ·
448c402
1
Parent(s): 6fa3cde
Fix: move admin assignment logic into main.py startup for reliability
Browse files- Dockerfile +2 -2
- app/main.py +24 -0
- hf_make_admin.py +0 -20
Dockerfile
CHANGED
|
@@ -31,5 +31,5 @@ ENV PORT=7860
|
|
| 31 |
# Expose port
|
| 32 |
EXPOSE ${PORT}
|
| 33 |
|
| 34 |
-
# Start uvicorn with configurable port
|
| 35 |
-
CMD
|
|
|
|
| 31 |
# Expose port
|
| 32 |
EXPOSE ${PORT}
|
| 33 |
|
| 34 |
+
# Start uvicorn with configurable port
|
| 35 |
+
CMD uvicorn app.main:app --host 0.0.0.0 --port ${PORT}
|
app/main.py
CHANGED
|
@@ -51,6 +51,30 @@ async def startup_event():
|
|
| 51 |
print("Initializing database...")
|
| 52 |
init_db()
|
| 53 |
print("Database initialized successfully!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
|
| 56 |
@app.on_event("shutdown")
|
|
|
|
| 51 |
print("Initializing database...")
|
| 52 |
init_db()
|
| 53 |
print("Database initialized successfully!")
|
| 54 |
+
|
| 55 |
+
# Check for ADMIN_EMAIL to grant admin privileges
|
| 56 |
+
import os
|
| 57 |
+
from sqlalchemy.orm import Session
|
| 58 |
+
from app.database.connection import SessionLocal
|
| 59 |
+
from app.database.models import User
|
| 60 |
+
|
| 61 |
+
admin_email = os.environ.get('ADMIN_EMAIL')
|
| 62 |
+
if admin_email:
|
| 63 |
+
db = SessionLocal()
|
| 64 |
+
try:
|
| 65 |
+
print(f"Checking admin status for: {admin_email}")
|
| 66 |
+
user = db.query(User).filter(User.email == admin_email).first()
|
| 67 |
+
if user:
|
| 68 |
+
if not user.is_admin:
|
| 69 |
+
user.is_admin = 1
|
| 70 |
+
db.commit()
|
| 71 |
+
print(f"✅ User {admin_email} has been granted ADMIN privileges!")
|
| 72 |
+
else:
|
| 73 |
+
print(f"ℹ️ User {admin_email} is already an admin.")
|
| 74 |
+
else:
|
| 75 |
+
print(f"⚠️ User {admin_email} not found. Signup first to become admin.")
|
| 76 |
+
finally:
|
| 77 |
+
db.close()
|
| 78 |
|
| 79 |
|
| 80 |
@app.on_event("shutdown")
|
hf_make_admin.py
DELETED
|
@@ -1,20 +0,0 @@
|
|
| 1 |
-
import sqlite3
|
| 2 |
-
import os
|
| 3 |
-
|
| 4 |
-
# This script will run on HF Spaces and make the email provided in the ADMIN_EMAIL secret an admin
|
| 5 |
-
email = os.environ.get('ADMIN_EMAIL')
|
| 6 |
-
|
| 7 |
-
if not email:
|
| 8 |
-
print("❌ ADMIN_EMAIL environment variable not set. Please add it to HF Space Secrets.")
|
| 9 |
-
else:
|
| 10 |
-
try:
|
| 11 |
-
conn = sqlite3.connect('/app/data/database.db')
|
| 12 |
-
conn.execute("UPDATE users SET is_admin = 1 WHERE email = ?", (email,))
|
| 13 |
-
conn.commit()
|
| 14 |
-
if conn.total_changes > 0:
|
| 15 |
-
print(f"✅ User {email} is now an ADMIN!")
|
| 16 |
-
else:
|
| 17 |
-
print(f"⚠️ User {email} not found in database. Sign up with this email first.")
|
| 18 |
-
conn.close()
|
| 19 |
-
except Exception as e:
|
| 20 |
-
print(f"❌ Error: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|