120 lines
2.9 KiB
Python
120 lines
2.9 KiB
Python
"""Main FastAPI application"""
|
|
|
|
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from contextlib import asynccontextmanager
|
|
from typing import List
|
|
import json
|
|
|
|
from app.config import settings
|
|
from app.database import init_db
|
|
from app.api import api_router
|
|
|
|
|
|
class ConnectionManager:
|
|
"""WebSocket connection manager"""
|
|
|
|
def __init__(self):
|
|
self.active_connections: List[WebSocket] = []
|
|
|
|
async def connect(self, websocket: WebSocket):
|
|
await websocket.accept()
|
|
self.active_connections.append(websocket)
|
|
|
|
def disconnect(self, websocket: WebSocket):
|
|
self.active_connections.remove(websocket)
|
|
|
|
async def broadcast(self, message: dict):
|
|
"""Broadcast message to all connected clients"""
|
|
for connection in self.active_connections:
|
|
try:
|
|
await connection.send_json(message)
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
# Create connection manager
|
|
manager = ConnectionManager()
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
"""Lifespan events"""
|
|
# Startup
|
|
await init_db()
|
|
yield
|
|
# Shutdown
|
|
pass
|
|
|
|
|
|
# Create FastAPI app
|
|
app = FastAPI(
|
|
title="AI Code Review Agent",
|
|
description="AI агент для автоматического ревью Pull Request",
|
|
version="0.1.0",
|
|
lifespan=lifespan
|
|
)
|
|
|
|
# Configure CORS
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=settings.cors_origins,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Include API routes
|
|
app.include_router(api_router, prefix="/api")
|
|
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
"""Root endpoint"""
|
|
return {
|
|
"message": "AI Code Review Agent API",
|
|
"version": "0.1.0",
|
|
"docs": "/docs"
|
|
}
|
|
|
|
|
|
@app.get("/health")
|
|
async def health_check():
|
|
"""Health check endpoint"""
|
|
return {"status": "healthy"}
|
|
|
|
|
|
@app.websocket("/ws/reviews")
|
|
async def websocket_endpoint(websocket: WebSocket):
|
|
"""WebSocket endpoint for real-time review updates"""
|
|
await manager.connect(websocket)
|
|
try:
|
|
while True:
|
|
# Keep connection alive
|
|
data = await websocket.receive_text()
|
|
# Echo back or handle client messages if needed
|
|
await websocket.send_json({"type": "pong", "message": "connected"})
|
|
except WebSocketDisconnect:
|
|
manager.disconnect(websocket)
|
|
|
|
|
|
async def broadcast_review_update(review_id: int, event_type: str, data: dict = None):
|
|
"""Broadcast review update to all connected clients"""
|
|
message = {
|
|
"type": event_type,
|
|
"review_id": review_id,
|
|
"data": data or {}
|
|
}
|
|
await manager.broadcast(message)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run(
|
|
"app.main:app",
|
|
host=settings.host,
|
|
port=settings.port,
|
|
reload=settings.debug
|
|
)
|
|
|