GPQBot
1from functools import partial2
3from fastapi import FastAPI4from pybotx import Bot5
6from db.sqlalchemy import build_db_session_factory7from api.dependencies.bot import get_bot8from api.routers import router9from app.settings import settings10from app.caching import RedisRepo11from app.resources import strings12
13async def startup(bot: Bot) -> None:14# -- Bot --15await bot.startup()16
17# -- Database --18bot.state.db_session_factory = await build_db_session_factory()19
20# -- Redis --21bot.state.redis_repo = await RedisRepo.init(22dsn=settings.REDIS_DSN, prefix=strings.BOT_PROJECT_NAME23)24
25
26async def shutdown(bot: Bot) -> None:27# -- Bot --28await bot.shutdown()29
30# -- Redis --31await bot.state.redis_repo.close()32
33
34def get_application() -> FastAPI:35"""Create configured server application instance."""36bot = get_bot()37
38application = FastAPI(title=strings.BOT_PROJECT_NAME)39application.state.bot = bot40
41application.add_event_handler("startup", partial(startup, bot))42application.add_event_handler("shutdown", partial(shutdown, bot))43
44application.include_router(router)45
46return application47
48
49app = get_application()