Spaces:
Sleeping
Sleeping
| import os | |
| import firebase_admin | |
| from firebase_admin import credentials, firestore | |
| # Initialize Firebase Admin SDK | |
| def initialize_firebase(): | |
| """Initialize Firebase Admin SDK with service account credentials.""" | |
| # Get the path to serviceAccount.json in the root directory | |
| service_account_path = os.path.join( | |
| os.path.dirname(os.path.dirname(__file__)), | |
| "serviceAccount.json" | |
| ) | |
| # Check if Firebase is already initialized | |
| if not firebase_admin._apps: | |
| cred = credentials.Certificate(service_account_path) | |
| firebase_admin.initialize_app(cred) | |
| # Return Firestore client | |
| return firestore.client() | |
| # Create a global Firestore client instance | |
| try: | |
| db = initialize_firebase() | |
| except Exception as e: | |
| print(f"Warning: Failed to initialize Firebase: {e}") | |
| db = None | |