47 lines
1.3 KiB
Python
47 lines
1.3 KiB
Python
from collections.abc import AsyncIterable
|
|
|
|
from fastapi import FastAPI
|
|
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, async_sessionmaker
|
|
|
|
from api.depends import ( # new_unit_of_work,; new_user_repository,; new_user_service,
|
|
create_engine,
|
|
create_session_maker,
|
|
new_session,
|
|
)
|
|
from api.models import BaseModel
|
|
|
|
|
|
async def lifespan(app: FastAPI) -> AsyncIterable[None]:
|
|
engine = app.dependency_overrides[AsyncEngine]()
|
|
|
|
async with engine.begin() as conn:
|
|
await conn.run_sync(BaseModel.metadata.create_all)
|
|
|
|
yield
|
|
|
|
async with engine.begin() as conn:
|
|
await conn.run_sync(BaseModel.metadata.drop_all)
|
|
|
|
|
|
def init_dependencies(app: FastAPI) -> None:
|
|
app.dependency_overrides[AsyncEngine] = create_engine
|
|
app.dependency_overrides[async_sessionmaker[AsyncSession]] = create_session_maker
|
|
app.dependency_overrides[AsyncSession] = new_session
|
|
# app.dependency_overrides[UserRepository] = new_user_repository
|
|
# app.dependency_overrides[UnitOfWork] = new_unit_of_work
|
|
# app.dependency_overrides[UserService] = new_user_service
|
|
|
|
|
|
def create_app() -> FastAPI:
|
|
app = FastAPI(
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
# app.include_router(user_router)
|
|
|
|
init_dependencies(app)
|
|
return app
|
|
|
|
|
|
app = create_app()
|