from fastapi import FastAPI, HTTPException, Request, UploadFile, File as FastAPIFile from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import FileResponse, HTMLResponse, JSONResponse from fastapi.staticfiles import StaticFiles import os import asyncio from dotenv import load_dotenv from google import genai # Import routes from routes.websocket import router as websocket_router load_dotenv() app = FastAPI(title="Gemini Multimodal API") # --- 1. Get your secret API key --- APP_API_KEY = os.environ.get("API_KEY", None) # Add CORS middleware app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Supported MIME types and extensions for upload validation SUPPORTED_UPLOAD_MIME_TYPES = { "application/pdf", "text/plain", "text/markdown", "text/html", "audio/mpeg", "audio/mp3", "audio/wav", "audio/ogg", "audio/flac", "video/mp4", "video/mpeg", "video/webm", "video/quicktime", "image/jpeg", "image/png", "image/webp", "image/gif", } SUPPORTED_UPLOAD_EXTENSIONS = { ".pdf", ".txt", ".md", ".html", ".mp3", ".wav", ".ogg", ".flac", ".mp4", ".mpeg", ".webm", ".mov", ".jpeg", ".jpg", ".png", ".webp", ".gif", } # --- 2. A simple, password-protected landing page --- @app.get("/", response_class=HTMLResponse) async def landing_page(request: Request): # If no API key is set in the environment, allow everyone. if not APP_API_KEY: return FileResponse("../frontend/index.html") # Get the key from the URL's query parameters api_key = request.query_params.get("api_key") # Check if the provided key is valid if api_key and api_key == APP_API_KEY: # Key is valid! Serve the real application. return FileResponse("../frontend/index.html") else: # Key is missing or invalid. Show a simple login page. return """
This application is protected. Please enter the access key.