rag_saas / app /main.py
GodRad's picture
Upload 37 files
758afe5 verified
Raw History Blame Contribute Delete
3.77 kB
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="""
<html>
<head><title>RAG SaaS</title></head>
<body style="font-family: sans-serif; display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; background: #f0f2f5;">
<div style="padding: 2rem; background: white; border-radius: 8px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); text-align: center;">
<h1 style="color: #1a73e8;">RAG Systems as a Service</h1>
<p>Enterprise-grade retrieval augmented generation platform.</p>
<div style="margin-top: 1rem;">
<a href="/docs" style="display: inline-block; padding: 0.5rem 1rem; background: #1a73e8; color: white; text-decoration: none; border-radius: 4px;">Explore API Docs</a>
<a href="/debug/routes" style="display: inline-block; margin-left: 0.5rem; padding: 0.5rem 1rem; border: 1px solid #1a73e8; color: #1a73e8; text-decoration: none; border-radius: 4px;">Debug Routes</a>
</div>
</div>
</body>
</html>
""")
@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}")