Files
piaps-course-work-university/backend/app/main.py

68 lines
2.1 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 contextlib import asynccontextmanager
from typing import AsyncGenerator
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from loguru import logger
from app.auth import router as auth_router
from app.routers import all_routers
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator[dict, None]:
"""Управление жизненным циклом приложения."""
logger.info("Инициализация приложения...")
yield
logger.info("Завершение работы приложения...")
def create_app() -> FastAPI:
"""
Создание и конфигурация FastAPI приложения.
Returns:
Сконфигурированное приложение FastAPI
"""
app = FastAPI(
title="Стартовая сборка FastAPI",
description=(
"Стартовая сборка с интегрированной SQLAlchemy 2 для разработки FastAPI приложений с продвинутой "
"архитектурой, включающей авторизацию, аутентификацию и управление ролями пользователей."
),
version="1.0.0",
lifespan=lifespan,
)
# Настройка CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Монтирование статических файлов
app.mount("/static", StaticFiles(directory="app/static"), name="static")
# Регистрация роутеров
register_routers(app)
return app
def register_routers(app: FastAPI) -> None:
"""Регистрация роутеров приложения."""
# Подключение роутеров
app.include_router(auth_router, prefix="/auth", tags=["Auth"])
for router in all_routers:
app.include_router(router)
# Создание экземпляра приложения
app = create_app()