FastAPI multiple StreamRouters #677
-
I'm struggling to get around the circular dependencies issue for FastAPI. app.main - I create the faststream.rabbit.fastapi.RabbitRouter and I include it as a router for the app. app.user.api - I can't import the router here, because these routes won't get included in app even though "include_router" happens. I can use the broker() function as a dependency, but that doesn't matter, because I still need a router. If I import the router from app.user.api to app.main then it's a circular dependency issue, because I'm using broker() as a dependency injection. The only way I'm getting this to work is if I keep all my API routes on app.main. Otherwise, they don't get included in the app's routes, or I receive a circular dependency. I believe this creates a broker each time the RabbitRouter is initialized, if not, then that makes things easy and I can just use it as a regular APIRouter for FastAPI. I was looking through the codebase, and it looks like there might be a high-level abstraction to aio-pika's Connection class to potentially draw from as a Singleton? If I'm able to just declare the Connection in its own module and then implement the connection through the brokers and routers that would help! Any tips or advice? Thanks! This project rocks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 15 replies
-
As I understood you correct, you are trying to create broker in the same with FastAPI application file (app.main) and declare some handler in the other file. But you can create the broker in the same with handlers file (app.user.api) and just import it in the app.main |
Beta Was this translation helpful? Give feedback.
-
@Lancetnik Thanks for clarifying. Is there already a tentative date / timeline for supporting multiple brokers? |
Beta Was this translation helpful? Give feedback.
As I understood you correct, you are trying to create broker in the same with FastAPI application file (app.main) and declare some handler in the other file. But you can create the broker in the same with handlers file (app.user.api) and just import it in the app.main