airtai / faststream

FastStream is a powerful and easy-to-use Python framework for building asynchronous services interacting with event streams such as Apache Kafka, RabbitMQ, NATS and Redis.
https://faststream.airt.ai/latest/
Apache License 2.0
2.2k stars 106 forks source link

Feature: allow including a`faststream.broker.router.BrokerRouter()` in a `StreamRouter` #1168

Open strangemonad opened 5 months ago

strangemonad commented 5 months ago

Is your feature request related to a problem? Please describe.

Consider the following scenario: You have an existing module that's based on a BrokerRouter (e.g. a NatsRouter) and it only has subscribers and publishers. You want to be able to re-use that module in plain FastStream apps and other apps (e.g. a FastAPI app). This means that at the top-level, you need to do something like

app_router = faststream.nats.fastapi.NatsRouter() # Any subclass of StreamRouter
app_router.include_router(shared_broker_routes.router) # This raises because `BrokerRouter.routes` doesn't exist.
app_router.include_router(shared_api_routes.router)
app = FastAPI()
# ...

Describe the solution you'd like It would be nice to not have to change existing BrokerRouter routes

a few options might be

Describe alternatives you've considered I currently can't directly share the code, I have to force everything to be a fastapi StreamRouter.

Lancetnik commented 5 months ago

Thanks for the Idea!

But, I am not sure, that is required due Stream Routers' can include each over. But it is a great way to reuse your regular FastStream routers in Fast API application... I need to think and take a look on implementation. Added to backlog, but it is still feature to discuss.