forked from YourRAG/YourRAG
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
85 lines (71 loc) · 1.99 KB
/
api.py
File metadata and controls
85 lines (71 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""Main API entry point - FastAPI application with route registration."""
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager
from auth import prisma
from redis_service import RedisService
from config_service import config_service
from routes import (
auth_router,
documents_router,
admin_router,
chat_router,
codebase_router,
credits_router,
redemption_router,
export_router,
source_search_router,
)
import config
import uvicorn
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Application lifespan handler for startup and shutdown."""
# Startup
await prisma.connect()
try:
await RedisService.connect()
except Exception as e:
print(f"Failed to connect to Redis: {e}")
# Load system configuration
await config_service.load_config()
yield
# Shutdown
await RedisService.disconnect()
await prisma.disconnect()
# Disable docs in production
docs_url = "/docs" if config.ENVIRONMENT == "development" else None
redoc_url = "/redoc" if config.ENVIRONMENT == "development" else None
app = FastAPI(
title="RAG API",
lifespan=lifespan,
docs_url=docs_url,
redoc_url=redoc_url
)
# CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Register routes
app.include_router(auth_router)
app.include_router(documents_router)
app.include_router(admin_router)
app.include_router(chat_router)
app.include_router(codebase_router)
app.include_router(credits_router)
app.include_router(redemption_router)
app.include_router(export_router)
app.include_router(source_search_router)
if __name__ == "__main__":
uvicorn.run(
"api:app",
host="0.0.0.0",
port=8000,
reload=config.ENVIRONMENT == "development",
reload_dirs=["."],
reload_excludes=["web/*", ".git/*", "prisma/*", "__pycache__/*"],
)