| """ |
| Main FastAPI application entry point. |
| """ |
| from fastapi import FastAPI, WebSocket |
| from fastapi.middleware.cors import CORSMiddleware |
| from contextlib import asynccontextmanager |
|
|
| from .config import settings |
| from .routes import auth_router, user_router, chat_router, group_router, system_router |
| from .sockets.ws import websocket_endpoint |
|
|
|
|
| @asynccontextmanager |
| async def lifespan(app: FastAPI): |
| """Application lifespan - startup and shutdown.""" |
| |
| print(f"Starting {settings.APP_NAME} v{settings.APP_VERSION}") |
| |
| |
| |
| |
| |
| |
| yield |
| |
| |
| print("Shutting down...") |
| |
| |
|
|
|
|
| |
| app = FastAPI( |
| title=settings.APP_NAME, |
| version=settings.APP_VERSION, |
| description="Chat application with async message processing", |
| lifespan=lifespan |
| ) |
|
|
| |
| app.add_middleware( |
| CORSMiddleware, |
| allow_origins=["*"], |
| allow_credentials=True, |
| allow_methods=["*"], |
| allow_headers=["*"], |
| ) |
|
|
|
|
| |
| app.include_router(auth_router, prefix="/api") |
| app.include_router(user_router, prefix="/api") |
| app.include_router(chat_router, prefix="/api") |
| app.include_router(group_router, prefix="/api") |
| app.include_router(system_router, prefix="/api") |
|
|
|
|
| |
| @app.websocket("/ws/{user_id}") |
| async def websocket_route(websocket: WebSocket, user_id: str): |
| """WebSocket endpoint for real-time notifications.""" |
| await websocket_endpoint(websocket, user_id) |
|
|
|
|
| |
| @app.get("/") |
| async def root(): |
| """Root endpoint.""" |
| return { |
| "name": settings.APP_NAME, |
| "version": settings.APP_VERSION, |
| "status": "running" |
| } |
|
|
|
|
| @app.get("/api/health") |
| async def health(): |
| """Health check endpoint.""" |
| return {"status": "healthy"} |
|
|
|
|
| |
| __all__ = ["app"] |