service_man/api/app_entrypoint/main.py

51 lines
1.6 KiB
Python
Raw Normal View History

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-04-10 00:33:31 +03:00
from api.app_entrypoint.dependencies import init_dependencies
from api.app_entrypoint.error_handlers import init_exc_handlers
2024-04-08 00:31:15 +03:00
from api.infrastructure.auth.jwt_settings import JwtSettings
2024-03-31 21:20:36 +03:00
from api.infrastructure.dependencies.adapters import create_engine
2024-04-23 12:08:56 +03:00
from api.infrastructure.dependencies.configs import (
app_settings,
get_db_settings,
get_jwt_settings,
)
2024-04-08 00:31:15 +03:00
from api.infrastructure.persistence.db_setings import DBSettings
2024-03-31 21:20:36 +03:00
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:
2024-04-08 00:31:15 +03:00
app.dependency_overrides[DBSettings] = get_db_settings
app.dependency_overrides[JwtSettings] = get_jwt_settings
2024-03-31 21:20:36 +03:00
app.dependency_overrides[Settings] = app_settings
app.dependency_overrides[AsyncEngine] = create_engine
2024-04-08 00:31:15 +03:00
engine = app.dependency_overrides[AsyncEngine](
app.dependency_overrides[Settings](
app.dependency_overrides[DBSettings](),
app.dependency_overrides[JwtSettings](),
),
)
2024-03-31 21:20:36 +03:00
async with engine.begin() as conn:
2024-04-01 12:19:10 +03:00
await conn.run_sync(Base.metadata.drop_all)
2024-03-31 21:20:36 +03:00
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-04-08 00:31:15 +03:00
init_exc_handlers(app)
2024-03-31 04:18:41 +03:00
2024-03-31 00:58:43 +03:00
return app