db_kyc_project/batcher/app/main.py

47 lines
1.3 KiB
Python

import aio_pika
from fastapi import Depends, FastAPI, Request, Response
from fastapi.middleware.cors import CORSMiddleware
from functools import partial
from starlette.exceptions import HTTPException
from contextlib import asynccontextmanager
from app.src.routers.api import router as router_api
from app.src.routers.handlers import http_error_handler
from app.src.domain.setting import launch_consumer
from app.src.db import connect_pg, get_connection, get_channel, get_rmq, get_pg
@asynccontextmanager
async def lifespan(app: FastAPI):
launch_consumer(connect_pg, get_connection)
app.state.pg_pool = await connect_pg()
rmq_conn_pool = aio_pika.pool.Pool(get_connection, max_size=2)
rmq_chan_pool = aio_pika.pool.Pool(partial(get_channel, conn_pool=rmq_conn_pool), max_size=10)
app.state.rmq_chan_pool = rmq_chan_pool
yield
await app.state.pg_pool.close()
def get_application() -> FastAPI:
application = FastAPI(lifespan=lifespan)
application.include_router(router_api, prefix='/api')
application.add_exception_handler(HTTPException, http_error_handler)
application.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
return application
app = get_application()