You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.3 KiB
80 lines
2.3 KiB
from fastapi import FastAPI, Request, status |
|
from fastapi.responses import JSONResponse |
|
from fastapi.staticfiles import StaticFiles |
|
from fastapi.middleware.cors import CORSMiddleware |
|
from sqlalchemy.exc import SQLAlchemyError |
|
import logging |
|
|
|
from apiApp.config import APP_TITLE, APP_VERSION, API_V1_PREFIX, UPLOAD_FOLDER, DATABASE_URL |
|
from apiApp.database import engine, Base |
|
from apiApp.routers import audio_router, recognition_router |
|
|
|
# Настройка логирования |
|
logging.basicConfig(level=logging.INFO) |
|
logger = logging.getLogger(__name__) |
|
|
|
# Создание FastAPI приложения |
|
app = FastAPI( |
|
title=APP_TITLE, |
|
version=APP_VERSION, |
|
docs_url=f"{API_V1_PREFIX}/docs", |
|
redoc_url=f"{API_V1_PREFIX}/redoc", |
|
openapi_url=f"{API_V1_PREFIX}/openapi.json" |
|
) |
|
|
|
# CORS middleware |
|
app.add_middleware( |
|
CORSMiddleware, |
|
allow_origins=["*"], |
|
allow_credentials=True, |
|
allow_methods=["*"], |
|
allow_headers=["*"], |
|
) |
|
|
|
|
|
# Глобальный обработчик ошибок SQLAlchemy |
|
@app.exception_handler(SQLAlchemyError) |
|
async def sqlalchemy_exception_handler(request: Request, exc: SQLAlchemyError): |
|
logger.error(f"Database error: {exc}") |
|
return JSONResponse( |
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, |
|
content={"detail": "Database error occurred"}, |
|
) |
|
|
|
|
|
# Создание таблиц |
|
@app.on_event("startup") |
|
async def startup_event(): |
|
"""Создание таблиц при запуске приложения""" |
|
try: |
|
Base.metadata.create_all(bind=engine) |
|
logger.info("Database tables created successfully") |
|
except Exception as e: |
|
logger.error(f"Error creating database tables: {e}") |
|
|
|
|
|
# Подключение routers |
|
app.include_router(audio_router, prefix=API_V1_PREFIX, tags=["audio"]) |
|
app.include_router(recognition_router, prefix=API_V1_PREFIX, tags=["recognition"]) |
|
|
|
# Статические файлы (для загрузки аудио) |
|
app.mount("/uploads", StaticFiles(directory=str(UPLOAD_FOLDER)), name="uploads") |
|
|
|
|
|
@app.get("/") |
|
async def root(): |
|
return { |
|
"application": APP_TITLE, |
|
"version": APP_VERSION, |
|
"status": "running" |
|
} |
|
|
|
|
|
@app.get("/health") |
|
async def health_check(): |
|
return {"status": "healthy"} |
|
|
|
|
|
if __name__ == "__main__": |
|
import uvicorn |
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|