dressed_for_succes_store/backend/app/main.py

57 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import JSONResponse
import os
from pathlib import Path
from app.config import settings
from app.routers import router
from app.core import Base, engine
# Создаем таблицы в базе данных
Base.metadata.create_all(bind=engine)
# Создаем экземпляр приложения FastAPI
app = FastAPI(
title=settings.APP_NAME,
description=settings.APP_DESCRIPTION,
version=settings.APP_VERSION
)
# Настраиваем CORS
app.add_middleware(
CORSMiddleware,
allow_origins=settings.CORS_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Обработчик исключений
@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
# В реальном приложении здесь должно быть логирование ошибок
return JSONResponse(
status_code=500,
content={"detail": "Внутренняя ошибка сервера"}
)
# Подключаем роутеры
app.include_router(router, prefix="/api")
# Создаем директорию для загрузок, если она не существует
uploads_dir = Path(settings.UPLOAD_DIRECTORY)
uploads_dir.mkdir(parents=True, exist_ok=True)
# Монтируем статические файлы
app.mount("/uploads", StaticFiles(directory=settings.UPLOAD_DIRECTORY), name="uploads")
# Корневой маршрут
@app.get("/")
async def root():
return {
"message": "Добро пожаловать в API интернет-магазина",
"docs_url": "/docs",
"redoc_url": "/redoc"
}