service_man/api/app.py

47 lines
1.3 KiB
Python
Raw Normal View History

2024-03-29 00:29:35 +03:00
from collections.abc import AsyncIterable
2024-03-04 07:12:29 +03:00
from fastapi import FastAPI
2024-03-29 00:29:35 +03:00
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)
2024-03-04 07:12:29 +03:00
2024-03-29 00:29:35 +03:00
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
2024-03-04 07:12:29 +03:00
def create_app() -> FastAPI:
2024-03-29 00:29:35 +03:00
app = FastAPI(
lifespan=lifespan,
)
# app.include_router(user_router)
2024-03-11 07:48:04 +03:00
2024-03-29 00:29:35 +03:00
init_dependencies(app)
2024-03-04 07:12:29 +03:00
return app
app = create_app()