Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,18 @@
|
|
| 1 |
from flask import Flask, jsonify
|
| 2 |
from flask_cors import CORS
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
-
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from db import db
|
| 6 |
import requests
|
| 7 |
|
|
@@ -9,49 +20,66 @@ load_dotenv()
|
|
| 9 |
app = Flask(__name__)
|
| 10 |
CORS(app)
|
| 11 |
|
| 12 |
-
# Import blueprints
|
| 13 |
-
|
| 14 |
-
from routes.
|
| 15 |
-
from routes.
|
| 16 |
-
from routes.
|
| 17 |
-
from routes.
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
@app.route("/")
|
| 26 |
def home():
|
|
|
|
| 27 |
return {"status": "Backend running"}
|
| 28 |
-
|
| 29 |
@app.route("/seed-db")
|
| 30 |
def seed_db():
|
| 31 |
try:
|
| 32 |
import seed
|
| 33 |
-
|
| 34 |
return {"status": "Seeding completed!"}
|
| 35 |
except Exception as e:
|
|
|
|
| 36 |
return {"status": "Seeding failed", "error": str(e)}
|
| 37 |
-
|
| 38 |
@app.route("/test-db")
|
| 39 |
def test_db():
|
| 40 |
try:
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 42 |
return {
|
| 43 |
"status": "Connected to MongoDB",
|
| 44 |
-
"users_count":
|
| 45 |
-
"courses_count":
|
| 46 |
-
"feedback_count":
|
| 47 |
}
|
| 48 |
except Exception as e:
|
| 49 |
-
|
| 50 |
return {"status": "DB connection failed", "error": str(e)}
|
| 51 |
|
| 52 |
# ✅ Debug route to check env vars
|
| 53 |
@app.route("/debug-env")
|
| 54 |
def debug_env():
|
|
|
|
| 55 |
return {
|
| 56 |
"mongo_uri_present": bool(os.environ.get("MONGO_URI")),
|
| 57 |
"mongo_db_present": bool(os.environ.get("MONGO_DB"))
|
|
@@ -60,23 +88,27 @@ def debug_env():
|
|
| 60 |
# ✅ Self-test route: checks if all main routes are reachable
|
| 61 |
@app.route("/self-test")
|
| 62 |
def self_test():
|
|
|
|
| 63 |
base_url = f"http://localhost:{os.environ.get('PORT', 7860)}"
|
| 64 |
-
routes = [
|
| 65 |
-
"/", "/test-db", "/debug-env", "/auth", "/courses", "/feedback", "/profile", "/admin"
|
| 66 |
-
]
|
| 67 |
results = {}
|
| 68 |
for r in routes:
|
| 69 |
try:
|
| 70 |
res = requests.get(base_url + r, timeout=5)
|
|
|
|
| 71 |
results[r] = {
|
| 72 |
"status_code": res.status_code,
|
| 73 |
"ok": res.ok,
|
| 74 |
"response": res.json() if res.headers.get("Content-Type", "").startswith("application/json") else res.text
|
| 75 |
}
|
| 76 |
except Exception as e:
|
|
|
|
| 77 |
results[r] = {"error": str(e)}
|
| 78 |
return jsonify(results)
|
| 79 |
|
|
|
|
|
|
|
| 80 |
if __name__ == "__main__":
|
| 81 |
port = int(os.environ.get("PORT", 7860))
|
|
|
|
| 82 |
app.run(host="0.0.0.0", port=port, debug=True)
|
|
|
|
| 1 |
from flask import Flask, jsonify
|
| 2 |
from flask_cors import CORS
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
+
import os, sys, logging
|
| 5 |
+
|
| 6 |
+
# Configure logging so Hugging Face (gunicorn) always shows it
|
| 7 |
+
logging.basicConfig(
|
| 8 |
+
level=logging.INFO,
|
| 9 |
+
format="%(asctime)s [%(levelname)s] %(message)s",
|
| 10 |
+
handlers=[logging.StreamHandler(sys.stdout)]
|
| 11 |
+
)
|
| 12 |
+
logger = logging.getLogger(__name__)
|
| 13 |
+
|
| 14 |
+
logger.info(">>> Starting app.py import...")
|
| 15 |
+
|
| 16 |
from db import db
|
| 17 |
import requests
|
| 18 |
|
|
|
|
| 20 |
app = Flask(__name__)
|
| 21 |
CORS(app)
|
| 22 |
|
| 23 |
+
# Import blueprints with error handling
|
| 24 |
+
try:
|
| 25 |
+
from routes.auth import auth_bp
|
| 26 |
+
from routes.courses import courses_bp
|
| 27 |
+
from routes.feedback import feedback_bp
|
| 28 |
+
from routes.profile import profile_bp
|
| 29 |
+
from routes.admin import admin_bp
|
| 30 |
+
logger.info(">>> Successfully imported all blueprints")
|
| 31 |
+
except Exception as e:
|
| 32 |
+
logger.exception("!!! Failed importing blueprints")
|
| 33 |
+
raise e
|
| 34 |
+
|
| 35 |
+
# Register blueprints
|
| 36 |
+
try:
|
| 37 |
+
app.register_blueprint(auth_bp, url_prefix="/auth")
|
| 38 |
+
app.register_blueprint(courses_bp, url_prefix="/courses")
|
| 39 |
+
app.register_blueprint(feedback_bp, url_prefix="/feedback")
|
| 40 |
+
app.register_blueprint(profile_bp, url_prefix="/profile")
|
| 41 |
+
app.register_blueprint(admin_bp, url_prefix="/admin")
|
| 42 |
+
logger.info(">>> All blueprints registered successfully")
|
| 43 |
+
except Exception as e:
|
| 44 |
+
logger.exception("!!! Failed registering blueprints")
|
| 45 |
+
raise e
|
| 46 |
+
|
| 47 |
@app.route("/")
|
| 48 |
def home():
|
| 49 |
+
logger.info(">>> / endpoint called")
|
| 50 |
return {"status": "Backend running"}
|
| 51 |
+
|
| 52 |
@app.route("/seed-db")
|
| 53 |
def seed_db():
|
| 54 |
try:
|
| 55 |
import seed
|
| 56 |
+
logger.info(">>> /seed-db route executed successfully")
|
| 57 |
return {"status": "Seeding completed!"}
|
| 58 |
except Exception as e:
|
| 59 |
+
logger.exception("!!! /seed-db failed")
|
| 60 |
return {"status": "Seeding failed", "error": str(e)}
|
| 61 |
+
|
| 62 |
@app.route("/test-db")
|
| 63 |
def test_db():
|
| 64 |
try:
|
| 65 |
+
count_users = db["users"].count_documents({})
|
| 66 |
+
count_courses = db["courses"].count_documents({})
|
| 67 |
+
count_feedback = db["feedback"].count_documents({})
|
| 68 |
+
logger.info(f">>> /test-db OK → users={count_users}, courses={count_courses}, feedback={count_feedback}")
|
| 69 |
return {
|
| 70 |
"status": "Connected to MongoDB",
|
| 71 |
+
"users_count": count_users,
|
| 72 |
+
"courses_count": count_courses,
|
| 73 |
+
"feedback_count": count_feedback
|
| 74 |
}
|
| 75 |
except Exception as e:
|
| 76 |
+
logger.exception("!!! /test-db failed")
|
| 77 |
return {"status": "DB connection failed", "error": str(e)}
|
| 78 |
|
| 79 |
# ✅ Debug route to check env vars
|
| 80 |
@app.route("/debug-env")
|
| 81 |
def debug_env():
|
| 82 |
+
logger.info(">>> /debug-env called")
|
| 83 |
return {
|
| 84 |
"mongo_uri_present": bool(os.environ.get("MONGO_URI")),
|
| 85 |
"mongo_db_present": bool(os.environ.get("MONGO_DB"))
|
|
|
|
| 88 |
# ✅ Self-test route: checks if all main routes are reachable
|
| 89 |
@app.route("/self-test")
|
| 90 |
def self_test():
|
| 91 |
+
logger.info(">>> /self-test route called")
|
| 92 |
base_url = f"http://localhost:{os.environ.get('PORT', 7860)}"
|
| 93 |
+
routes = ["/", "/test-db", "/debug-env", "/auth", "/courses", "/feedback", "/profile", "/admin"]
|
|
|
|
|
|
|
| 94 |
results = {}
|
| 95 |
for r in routes:
|
| 96 |
try:
|
| 97 |
res = requests.get(base_url + r, timeout=5)
|
| 98 |
+
logger.info(f"Checked {r} → {res.status_code}")
|
| 99 |
results[r] = {
|
| 100 |
"status_code": res.status_code,
|
| 101 |
"ok": res.ok,
|
| 102 |
"response": res.json() if res.headers.get("Content-Type", "").startswith("application/json") else res.text
|
| 103 |
}
|
| 104 |
except Exception as e:
|
| 105 |
+
logger.warning(f"!!! Error checking {r}: {e}")
|
| 106 |
results[r] = {"error": str(e)}
|
| 107 |
return jsonify(results)
|
| 108 |
|
| 109 |
+
logger.info(">>> Finished loading app.py (Gunicorn should now serve the app)")
|
| 110 |
+
|
| 111 |
if __name__ == "__main__":
|
| 112 |
port = int(os.environ.get("PORT", 7860))
|
| 113 |
+
logger.info(f">>> Running Flask dev server on 0.0.0.0:{port}")
|
| 114 |
app.run(host="0.0.0.0", port=port, debug=True)
|