dressed_for_succes_store/backend/app/main.py
2025-04-03 23:17:57 +07:00

46 lines
1.5 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.

import os
from pathlib import Path
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from fastapi.staticfiles import StaticFiles
from sqlalchemy.exc import SQLAlchemyError
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="Dressed for Success API",
description="API для интернет-магазина одежды",
version="1.0.0"
)
# Настраиваем CORS
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):
return JSONResponse(
status_code=500,
content={"detail": "Ошибка базы данных", "error": str(exc)},
)
# Подключаем роутеры
app.include_router(router, prefix="/api")
# Корневой маршрут
@app.get("/")
async def root():
return {"message": "Добро пожаловать в API интернет-магазина Dressed for Success"}