File size: 769 Bytes
35b7fa9
 
 
 
 
2f595c7
 
35b7fa9
2f595c7
35b7fa9
2f595c7
24158c3
35b7fa9
 
2f595c7
 
 
 
24158c3
 
2f595c7
 
35b7fa9
24158c3
 
 
35b7fa9
2f595c7
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
29
import firebase_admin
from firebase_admin import credentials, firestore
import os
import json

db = None

def init_db():
    global db
    if not firebase_admin._apps:
        config = os.getenv("FIREBASE_CONFIG")
        json_path = "serviceAccountKey.json"

        try:
            if config:
                cred = credentials.Certificate(json.loads(config))
                firebase_admin.initialize_app(cred)
                db = firestore.client()
            elif os.path.exists(json_path):
                cred = credentials.Certificate(json_path)
                firebase_admin.initialize_app(cred)
                db = firestore.client()
        except Exception as e:
            print(f"DB Error: {e}")
    else:
        db = firestore.client()

init_db()