Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 39 additions & 24 deletions server/python/main.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,34 @@
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from src.routers import movies
from src.utils.errorHandler import register_error_handlers
from src.database.mongo_client import db, get_collection
import traceback

import os
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

app = FastAPI()

# Add CORS middleware
cors_origins = os.getenv("CORS_ORIGINS", "http://localhost:3000,http://localhost:3001").split(",")
app.add_middleware(
CORSMiddleware,
allow_origins=[origin.strip() for origin in cors_origins], # Load from environment variable
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@asynccontextmanager
async def lifespan(app: FastAPI):
# Startup: Create search indexes
await ensure_search_index()
await vector_search_index()
yield
# Shutdown: Clean up resources if needed
# Add any cleanup code here

register_error_handlers(app)
app.include_router(movies.router, prefix="/api/movies", tags=["movies"])


@app.on_event("startup")
async def ensure_search_index():
try:
movies_collection = db.get_collection("movies")
result = await movies_collection.list_search_indexes()
indexes = [idx async for idx in result]
index_names = [index["name"] for index in indexes]
if "movieSearchIndex" in index_names:
print("MongoDB Search index already exists.")
return

# Create a mapping if the movieSearchIndex does not exist
Expand All @@ -58,18 +52,20 @@ async def ensure_search_index():
"definition": index_definition
}]
})
print("MongoDB Search index created.")
except Exception as e:
print(f"Error creating the search index: {e}")
raise RuntimeError(
f"Failed to create search index 'movieSearchIndex': {str(e)}. "
f"Search functionality may not work properly. "
f"Please check your MongoDB Atlas configuration and ensure the cluster supports search indexes."
)


@app.on_event("startup")
async def vector_search_index():
"""
Creates vector search index on application startup if it doesn't already exist.
This ensures the index is ready before any vector search requests are made.
"""
try:

embedded_movies_collection = get_collection("embedded_movies")

# Get list of existing indexes - convert AsyncCommandCursor to list
Expand Down Expand Up @@ -97,10 +93,29 @@ async def vector_search_index():
}

# Create the index
result = await embedded_movies_collection.create_search_index(index_definition)
print("Vector search index 'vector_index' ready to query.")
await embedded_movies_collection.create_search_index(index_definition)

except Exception as e:
print(f"Error during vector search index setup: {str(e)}")
print(f"Error type: {type(e).__name__}")
raise RuntimeError(
f"Failed to create vector search index 'vector_index': {str(e)}. "
f"Vector search functionality will not be available. "
f"Please check your MongoDB Atlas configuration, ensure the cluster supports vector search, "
f"and verify the 'embedded_movies' collection exists with the required embedding field."
)


app = FastAPI(lifespan=lifespan)

# Add CORS middleware
cors_origins = os.getenv("CORS_ORIGINS", "http://localhost:3000,http://localhost:3001").split(",")
app.add_middleware(
CORSMiddleware,
allow_origins=[origin.strip() for origin in cors_origins], # Load from environment variable
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)

register_error_handlers(app)
app.include_router(movies.router, prefix="/api/movies", tags=["movies"])

Loading
Loading