2024-03-31 21:07:59 +03:00
|
|
|
from collections.abc import AsyncGenerator
|
|
|
|
from contextlib import asynccontextmanager
|
|
|
|
|
2024-03-31 00:58:43 +03:00
|
|
|
from fastapi import FastAPI
|
2024-03-31 21:20:36 +03:00
|
|
|
from sqlalchemy.ext.asyncio import AsyncEngine
|
2024-03-31 00:58:43 +03:00
|
|
|
|
2024-03-31 04:18:41 +03:00
|
|
|
from api.app_builder.dependencies import init_dependencies
|
2024-03-31 21:20:36 +03:00
|
|
|
from api.infrastructure.dependencies.adapters import create_engine
|
|
|
|
from api.infrastructure.dependencies.configs import app_settings
|
|
|
|
from api.infrastructure.persistence.models import Base
|
|
|
|
from api.infrastructure.settings import Settings
|
2024-03-31 04:18:41 +03:00
|
|
|
|
2024-03-31 00:58:43 +03:00
|
|
|
from .routers import init_routers
|
|
|
|
|
|
|
|
|
2024-03-31 21:07:59 +03:00
|
|
|
@asynccontextmanager
|
|
|
|
async def lifespan(app: FastAPI) -> AsyncGenerator:
|
|
|
|
print("init lifespan")
|
2024-03-31 21:20:36 +03:00
|
|
|
app.dependency_overrides[Settings] = app_settings
|
|
|
|
app.dependency_overrides[AsyncEngine] = create_engine
|
|
|
|
engine = app.dependency_overrides[AsyncEngine](app.dependency_overrides[Settings]())
|
|
|
|
|
|
|
|
async with engine.begin() as conn:
|
|
|
|
await conn.run_sync(Base.metadata.create_all)
|
2024-03-31 21:07:59 +03:00
|
|
|
yield
|
|
|
|
|
|
|
|
|
2024-03-31 00:58:43 +03:00
|
|
|
def app_factory() -> FastAPI:
|
2024-03-31 21:07:59 +03:00
|
|
|
app = FastAPI(
|
|
|
|
lifespan=lifespan,
|
|
|
|
)
|
2024-03-31 04:18:41 +03:00
|
|
|
init_dependencies(app)
|
2024-03-31 00:58:43 +03:00
|
|
|
init_routers(app)
|
2024-03-31 04:18:41 +03:00
|
|
|
|
2024-03-31 00:58:43 +03:00
|
|
|
return app
|