Update config.py
Browse files
config.py
CHANGED
@@ -7,6 +7,8 @@ from pyfcm import FCMNotification
|
|
7 |
from datetime import datetime
|
8 |
import httpx
|
9 |
import os
|
|
|
|
|
10 |
|
11 |
# Logging
|
12 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(name)s - %(message)s")
|
@@ -43,15 +45,20 @@ notifications_collection = db["notifications"]
|
|
43 |
logger.info("📡 Connected to MongoDB synchronously at %s", datetime.utcnow().isoformat())
|
44 |
|
45 |
# Retrieve secrets from environment variables (set in Hugging Face Space Secrets)
|
46 |
-
|
47 |
SECRET_KEY = os.getenv("SECRET_KEY")
|
48 |
|
49 |
-
if not
|
50 |
-
logger.error("❌ Missing
|
51 |
-
raise ValueError("
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
# FCM settings
|
54 |
-
push_service = FCMNotification(
|
55 |
|
56 |
# Auth Space URL
|
57 |
AUTH_SPACE_URL = "https://rocketfarmstudios-cps-api.hf.space/auth"
|
@@ -83,6 +90,10 @@ async def send_push_notification(recipient_email, message):
|
|
83 |
logger.info(f"Push notification sent to {recipient_email} at {datetime.utcnow().isoformat()}: {result}")
|
84 |
except Exception as e:
|
85 |
logger.error(f"Failed to send push notification to {recipient_email} at {datetime.utcnow().isoformat()}: {str(e)}")
|
|
|
|
|
|
|
|
|
86 |
|
87 |
# JWT settings (minimal implementation for token creation)
|
88 |
from datetime import timedelta
|
|
|
7 |
from datetime import datetime
|
8 |
import httpx
|
9 |
import os
|
10 |
+
import json
|
11 |
+
import tempfile
|
12 |
|
13 |
# Logging
|
14 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(name)s - %(message)s")
|
|
|
45 |
logger.info("📡 Connected to MongoDB synchronously at %s", datetime.utcnow().isoformat())
|
46 |
|
47 |
# Retrieve secrets from environment variables (set in Hugging Face Space Secrets)
|
48 |
+
FCM_SERVICE_ACCOUNT_JSON = os.getenv("FCM_SERVICE_ACCOUNT")
|
49 |
SECRET_KEY = os.getenv("SECRET_KEY")
|
50 |
|
51 |
+
if not FCM_SERVICE_ACCOUNT_JSON or not SECRET_KEY:
|
52 |
+
logger.error("❌ Missing FCM_SERVICE_ACCOUNT or SECRET_KEY in environment variables")
|
53 |
+
raise ValueError("FCM_SERVICE_ACCOUNT and SECRET_KEY must be set in Hugging Face Space Secrets")
|
54 |
+
|
55 |
+
# Write the FCM service account JSON to a temporary file
|
56 |
+
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.json') as temp_file:
|
57 |
+
json.dump(json.loads(FCM_SERVICE_ACCOUNT_JSON), temp_file)
|
58 |
+
temp_file_path = temp_file.name
|
59 |
|
60 |
# FCM settings
|
61 |
+
push_service = FCMNotification(service_account_file=temp_file_path)
|
62 |
|
63 |
# Auth Space URL
|
64 |
AUTH_SPACE_URL = "https://rocketfarmstudios-cps-api.hf.space/auth"
|
|
|
90 |
logger.info(f"Push notification sent to {recipient_email} at {datetime.utcnow().isoformat()}: {result}")
|
91 |
except Exception as e:
|
92 |
logger.error(f"Failed to send push notification to {recipient_email} at {datetime.utcnow().isoformat()}: {str(e)}")
|
93 |
+
finally:
|
94 |
+
# Clean up the temporary file
|
95 |
+
if os.path.exists(temp_file_path):
|
96 |
+
os.remove(temp_file_path)
|
97 |
|
98 |
# JWT settings (minimal implementation for token creation)
|
99 |
from datetime import timedelta
|