diff --git a/backend/main.py b/backend/main.py index 40b0e57..8997b45 100644 --- a/backend/main.py +++ b/backend/main.py @@ -122,18 +122,21 @@ async def lifespan(app: FastAPI): if not frontend_url: if is_production: - raise ValueError( - "FRONTEND_URL environment variable is required for security in production. " - "Set it to your frontend URL (e.g., https://your-app.netlify.app)." + logger.warning( + "FRONTEND_URL environment variable is MISSING in production. " + "Defaulting to wildcard '*' for CORS to allow startup. " + "PLEASE SET THIS IN RENDER DASHBOARD." ) + frontend_url = "*" # Allow all origins temporarily to fix deployment else: logger.warning("FRONTEND_URL not set. Defaulting to http://localhost:5173 for development.") frontend_url = "http://localhost:5173" -if not (frontend_url.startswith("http://") or frontend_url.startswith("https://")): - raise ValueError( - f"FRONTEND_URL must be a valid HTTP/HTTPS URL. Got: {frontend_url}" +if frontend_url != "*" and not (frontend_url.startswith("http://") or frontend_url.startswith("https://")): + logger.warning( + f"FRONTEND_URL format invalid: {frontend_url}. Expected HTTP/HTTPS URL. Using '*' as fallback." ) + frontend_url = "*" allowed_origins = [frontend_url] diff --git a/backend/requirements-render.txt b/backend/requirements-render.txt index 2b35287..564bac4 100644 --- a/backend/requirements-render.txt +++ b/backend/requirements-render.txt @@ -8,7 +8,6 @@ psycopg2-binary async-lru huggingface-hub httpx -python-magic pywebpush Pillow firebase-functions diff --git a/backend/utils.py b/backend/utils.py index 2e24849..4003248 100644 --- a/backend/utils.py +++ b/backend/utils.py @@ -8,7 +8,10 @@ import shutil import logging import io -import magic +try: + import magic +except ImportError: + magic = None from typing import Optional from backend.cache import user_upload_cache @@ -73,17 +76,18 @@ def _validate_uploaded_file_sync(file: UploadFile) -> Optional[Image.Image]: # Check MIME type from content using python-magic try: - # Read first 1024 bytes for MIME detection - file_content = file.file.read(1024) - file.file.seek(0) # Reset file pointer + if magic: + # Read first 1024 bytes for MIME detection + file_content = file.file.read(1024) + file.file.seek(0) # Reset file pointer - detected_mime = magic.from_buffer(file_content, mime=True) + detected_mime = magic.from_buffer(file_content, mime=True) - if detected_mime not in ALLOWED_MIME_TYPES: - raise HTTPException( - status_code=400, - detail=f"Invalid file type. Only image files are allowed. Detected: {detected_mime}" - ) + if detected_mime not in ALLOWED_MIME_TYPES: + raise HTTPException( + status_code=400, + detail=f"Invalid file type. Only image files are allowed. Detected: {detected_mime}" + ) # Additional content validation: Try to open with PIL to ensure it's a valid image try: @@ -158,15 +162,16 @@ def process_uploaded_image_sync(file: UploadFile) -> tuple[Image.Image, bytes]: # Check MIME type try: - file_content = file.file.read(1024) - file.file.seek(0) - detected_mime = magic.from_buffer(file_content, mime=True) + if magic: + file_content = file.file.read(1024) + file.file.seek(0) + detected_mime = magic.from_buffer(file_content, mime=True) - if detected_mime not in ALLOWED_MIME_TYPES: - raise HTTPException( - status_code=400, - detail=f"Invalid file type. Only image files are allowed. Detected: {detected_mime}" - ) + if detected_mime not in ALLOWED_MIME_TYPES: + raise HTTPException( + status_code=400, + detail=f"Invalid file type. Only image files are allowed. Detected: {detected_mime}" + ) try: img = Image.open(file.file) diff --git a/start-backend.py b/start-backend.py index 04f7e52..14f98f5 100644 --- a/start-backend.py +++ b/start-backend.py @@ -29,7 +29,8 @@ def validate_environment(): print(f" - {var}") print("\nPlease set these variables or create a .env file.") print("See backend/.env.example for reference.") - return False + # We don't return False here to allow the app to start and serve health check + print("⚠️ Proceeding with missing variables (some features may be broken)") # Set defaults for optional variables if not os.getenv("DATABASE_URL"): @@ -60,8 +61,7 @@ def main(): """Main startup function""" print("🚀 Starting VishwaGuru Backend") - if not validate_environment(): - sys.exit(1) + validate_environment() create_data_directory()