Spaces:
Sleeping
Sleeping
File size: 1,397 Bytes
9a6b7dc 97661a7 9a6b7dc 0759822 9a6b7dc |
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import base64
import json
import os
import firebase_admin
from firebase_admin import db
from dotenv import load_dotenv
load_dotenv()
def decode_service_key():
encoded_key = os.environ["FIREBASE_CREDENTIALS"]
original_service_key = json.loads(base64.b64decode(encoded_key).decode('utf-8'))
if original_service_key:
return original_service_key
return None
# Firebase initialization
def initialize_firebase():
try:
# Try to initialize Firebase app if not already initialized
firebase_admin.get_app()
except ValueError:
creds_file_name = '.config/user-evaluations-firebase-creds.json'
if not (os.path.exists(creds_file_name) and os.path.isdir(creds_file_name)):
credentials = decode_service_key()
with open(creds_file_name, 'w', encoding='utf-8') as file:
json.dump(credentials, file, ensure_ascii=False, indent=4)
# If not initialized, initialize the default app
cred_obj = firebase_admin.credentials.Certificate(creds_file_name)
database_URL = os.environ["FIREBASE_DATABASE_URL"]
firebase_admin.initialize_app(cred_obj, {'databaseURL': database_URL})
def db_setup():
try:
initialize_firebase()
except Exception as e:
print(f"Error initializing Firebase: {e}")
firebase_admin.get_app()
ref = db.reference('/')
return ref
|