import os import sys import shutil import logging from pathlib import Path # Set up logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # Add the backend directory to the Python path sys.path.append(os.path.abspath("backend")) # Create necessary directories if they don't exist os.makedirs("./temp_audio", exist_ok=True) os.makedirs("./temp", exist_ok=True) # Set environment variables for MongoDB connection timeout os.environ["MONGODB_CONNECT_TIMEOUT_MS"] = "5000" # 5 seconds timeout os.environ["MONGODB_SERVER_SELECTION_TIMEOUT_MS"] = "5000" # 5 seconds timeout # Create a FastAPI app - either the main one or an error one from fastapi import FastAPI, Request from fastapi.responses import HTMLResponse, JSONResponse from fastapi.staticfiles import StaticFiles from fastapi.middleware.cors import CORSMiddleware # Check if static directory exists with index.html static_path = Path("./static") static_index_exists = static_path.exists() and (static_path / "index.html").exists() logger.info(f"Static path exists: {static_path.exists()}") if static_path.exists(): logger.info(f"Static directory contents: {os.listdir(static_path)}") logger.info(f"Static index exists: {(static_path / 'index.html').exists()}") try: # Try to import the main app from backend.app.main import app # Add CORS middleware app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Add an index route to check if the API is accessible @app.get("/api/status") async def status(): return {"status": "ok", "message": "PodCraft API is running"} # Override the root route to serve the frontend @app.get("/", response_class=HTMLResponse) async def read_root(): # If static files exist, return the index.html file if static_index_exists: with open(static_path / "index.html") as f: return f.read() else: return """
The PodCraft API is running, but the frontend static files were not found.
Static directory: ./static
Static directory exists: {static_path.exists()}
If you need to access the API directly, you can use the following endpoints:
/api/status
- Check API status/docs
- API documentationAn error occurred while starting the application:
{str(e)}
Please check your environment variables and configuration.
MongoDB URL format should be: mongodb+srv://username:password@cluster.mongodb.net/...
API keys should be properly formatted and valid.
Static directory exists: {static_path.exists()}
Static directory contents: {os.listdir(static_path)}
Static index exists: {static_index_exists}