File size: 841 Bytes
356e05f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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