diff --git a/main.py b/main.py index 34e1e82..24de7c7 100644 --- a/main.py +++ b/main.py @@ -32,6 +32,9 @@ from ml.anomaly_detector import AnomalyDetector from auth.oauth_flow import get_authorization_url, exchange_code_for_token from auth.token_store import save_token + +from google.cloud import firestore +import firebase_admin from firebase_admin import credentials, messaging import base64 import json @@ -47,6 +50,19 @@ app = FastAPI( version="0.1.0", ) +# ── FIREBASE & FIRESTORE INIT ─────────────────────────────────────────────── +db = None +try: + # When running in Google Cloud, ADC will be used automatically. + # For local dev, `gcloud auth application-default login` is required. + firebase_admin.initialize_app() + db = firestore.Client() + print("Firestore client initialized successfully.") +except Exception as e: + # The app can still run in a limited mode without Firestore. + # Endpoints that depend on `db` will return a 500 error. + print(f"WARNING: Firestore client failed to initialize: {e}", file=sys.stderr) + # ── AUTH & SESSION ──────────────────────────────────────────────────────────── # Add session middleware for storing auth state app.add_middleware(SessionMiddleware, secret_key=os.environ.get("SESSION_SECRET"))