2024-12-13 16:45:21 +03:00
|
|
|
import aio_pika
|
2024-10-23 11:54:32 +03:00
|
|
|
from fastapi import Depends, FastAPI, Request, Response
|
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
2024-12-13 16:45:21 +03:00
|
|
|
from functools import partial
|
2024-10-23 11:54:32 +03:00
|
|
|
from starlette.exceptions import HTTPException
|
|
|
|
|
2024-12-13 16:45:21 +03:00
|
|
|
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
|
2025-05-11 10:49:39 +03:00
|
|
|
from app.src.db import connect_pg, get_connection, get_channel, get_rmq, get_pg
|
2024-10-23 11:54:32 +03:00
|
|
|
|
|
|
|
|
|
|
|
def get_application() -> FastAPI:
|
|
|
|
application = FastAPI()
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2024-12-13 16:45:21 +03:00
|
|
|
app = get_application()
|
|
|
|
|
|
|
|
@app.on_event("startup")
|
|
|
|
async def startup():
|
2025-05-11 10:49:39 +03:00
|
|
|
launch_consumer(connect_pg, get_connection)
|
2024-12-13 16:45:21 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
@app.on_event("shutdown")
|
|
|
|
async def shutdown():
|
|
|
|
await app.state.pg_pool.close()
|
2024-10-23 11:54:32 +03:00
|
|
|
|