from fastapi import FastAPI, Depends
from fastapi.responses import JSONResponse, HTMLResponse
from fastapi.middleware.cors import CORSMiddleware
from app.core.config import settings
from app.core.config import settings
from app.models.database import engine
from app.models.orm import Base
# Create tables
Base.metadata.create_all(bind=engine)
app = FastAPI(
title=settings.PROJECT_NAME,
openapi_url=f"{settings.API_V1_STR}/openapi.json",
)
# Add Debug Middleware to trace 403 Errors
@app.middleware("http")
async def debug_middleware(request, call_next):
origin = request.headers.get("origin")
method = request.method
path = request.url.path
print(f"DEBUG: {method} {path} | Origin: {origin}")
response = await call_next(request)
print(f"DEBUG: Response Status: {response.status_code}")
return response
# Add CORS Middleware
app.add_middleware(
CORSMiddleware,
allow_origin_regex=".*", # Allows all origins while supporting credentials
allow_credentials=True,
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allow_headers=["*"],
expose_headers=["*"],
)
@app.get("/health")
def health_check():
return {"status": "healthy"}
@app.get("/")
def root():
return HTMLResponse(content="""
RAG SaaS
RAG Systems as a Service
Enterprise-grade retrieval augmented generation platform.
""")
@app.get("/debug/routes")
def list_routes():
url_list = []
for route in app.routes:
url_list.append({
"path": route.path,
"name": route.name,
"methods": list(route.methods) if hasattr(route, "methods") else None
})
return {"total_routes": len(url_list), "routes": url_list}
# Explicitly import routers to avoid namespace issues
from app.api.v1.tenants import router as tenants_router
from app.api.v1.sources import router as sources_router
from app.api.v1.ingest import router as ingest_router
from app.api.v1.ask import router as ask_router
from app.api.v1.admin import router as admin_router
# Add routers with explicit prefixes
# API_V1_STR is likely "/v1"
app.include_router(tenants_router, prefix=f"{settings.API_V1_STR}/tenants", tags=["tenants"])
app.include_router(sources_router, prefix=f"{settings.API_V1_STR}/sources", tags=["sources"])
app.include_router(ingest_router, prefix=f"{settings.API_V1_STR}/ingest", tags=["ingest"])
app.include_router(ask_router, prefix=f"{settings.API_V1_STR}/ask", tags=["ask"])
app.include_router(admin_router, prefix=f"{settings.API_V1_STR}/admin", tags=["admin"])
# Print routes on startup for server logs
@app.on_event("startup")
def on_startup():
print("Registered Routes:")
for route in app.routes:
print(f"Path: {route.path}")