49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
"""FastAPI application factory."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import AsyncIterator
|
|
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI
|
|
|
|
from app.api.routes import api_router
|
|
from app.core.cache import init_cache, shutdown_cache
|
|
from app.core.config import settings
|
|
from app.core.middleware.cache_monitor import CacheAvailabilityMiddleware
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
|
def create_app() -> FastAPI:
|
|
"""Build FastAPI application instance."""
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
|
|
await init_cache()
|
|
try:
|
|
yield
|
|
finally:
|
|
await shutdown_cache()
|
|
|
|
application = FastAPI(title=settings.project_name, version=settings.version, lifespan=lifespan)
|
|
application.include_router(api_router)
|
|
application.add_middleware(CacheAvailabilityMiddleware)
|
|
application.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=[
|
|
# "https://kitchen-crm.k1nq.tech",
|
|
# "http://192.168.31.51",
|
|
# "http://localhost:8000",
|
|
# "http://0.0.0.0:8000",
|
|
# "http://127.0.0.1:8000",
|
|
"*" # ! TODO: Убрать
|
|
],
|
|
allow_credentials=True,
|
|
allow_methods=["*"], # Разрешить все HTTP-методы
|
|
allow_headers=["*"], # Разрешить все заголовки
|
|
)
|
|
return application
|
|
|
|
|
|
app = create_app()
|