Dmitry Afanasyev 2359481fb7
add auth context (#62)
* add user table and superuser creation

* add gpt-4-stream-aivvm provider

* rename user migration to auth migration
2023-11-28 23:06:26 +03:00

54 lines
1.3 KiB
Python

from typing import Awaitable, Callable
from fastapi import FastAPI
from infra.database.db_adapter import Database
def startup(app: FastAPI, database: Database) -> Callable[[], Awaitable[None]]:
"""
Actions to run on application startup.
This function use fastAPI app to store data,
such as db_engine.
:param app: the fastAPI application.
:param database: app settings
:return: function that actually performs actions.
"""
async def _startup() -> None:
_setup_db(app, database)
return _startup
def shutdown(app: FastAPI) -> Callable[[], Awaitable[None]]:
"""
Actions to run on application's shutdown.
:param app: fastAPI application.
:return: function that actually performs actions.
"""
async def _shutdown() -> None:
await app.state.db_engine.dispose()
return _shutdown
def _setup_db(app: FastAPI, database: Database) -> None:
"""
Create connection to the database.
This function creates SQLAlchemy engine instance,
session_factory for creating sessions
and stores them in the application's state property.
:param app: fastAPI application.
"""
app.state.db_engine = database.async_engine
app.state.db_session_factory = database._async_session_factory