/
mainframe
/
Intellectuality_task_manager
Обзор
Документация
Войти
/
mainframe
/
Intellectuality_task_manager
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
backend/app/main.py
102 строки
3 KB
Ivan
initial commit
14 апр 2026, 17:18
14 апр 2026, 17:18
d3c4609
Код
Авторство
О чём код?
# path: backend/app/main.py from fastapi import FastAPI, Request from fastapi.exceptions import RequestValidationError from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse from app.config import settings from app.exceptions import AppException from app.routers.ai import router as ai_router from app.routers.health import router as health_router from app.routers.tasks import router as tasks_router app = FastAPI( title=settings.app_title, version="1.0.0", ) app.add_middleware( CORSMiddleware, allow_origins=["*"] if settings.cors_origins == "*" else settings.cors_origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) @app.exception_handler(AppException) async def app_exception_handler( request: Request, exc: AppException, ) -> JSONResponse: """ Единая обработка прикладных ошибок. """ return JSONResponse( status_code=exc.status_code, content={ "error": { "code": exc.code, "message": exc.message, "details": exc.details, } }, ) @app.exception_handler(RequestValidationError) async def validation_exception_handler( request: Request, exc: RequestValidationError, ) -> JSONResponse: """ Единая обработка ошибок валидации. """ return JSONResponse( status_code=422, content={ "error": { "code": "validation_error", "message": "Ошибка валидации входных данных.", "details": exc.errors(), } }, ) @app.exception_handler(Exception) async def unhandled_exception_handler( request: Request, exc: Exception, ) -> JSONResponse: """ Единая обработка непредвиденных ошибок. """ return JSONResponse( status_code=500, content={ "error": { "code": "internal_server_error", "message": "Внутренняя ошибка сервера.", "details": str(exc), } }, ) @app.get("/") async def root() -> dict: """ Корневой эндпоинт. """ return { "service": settings.app_title, "version": "1.0.0", "api": settings.api_v1_prefix, } app.include_router(health_router) app.include_router(tasks_router, prefix=settings.api_v1_prefix) app.include_router(ai_router, prefix=settings.api_v1_prefix)