-
Notifications
You must be signed in to change notification settings - Fork 284
Description
Currently, users have to manually set up all incoming and outgoing topics in the @router.subscriber()
and @router.publisher()
decorators. It would be great if we could define topic configurations at the broker level.
If users want to specify topics from the settings, they have several options:
-
Use a global settings object:
@router.subscriber(settings.TOPIC)
-
Import a function and set it up in the main:
from routes import handler def main(): broker = Broker() broker.subscriber(handler)
-
The closest option is using
Route
: https://faststream.ag2.ai/latest/getting-started/routers/#delay-handler-registration
All these alternatives require additional work from the user and are not as declarative as we'd like.
Therefore, I propose providing users with a more flexible configuration option. Something like this:
@router.subscriber(Config("IN_TOPIC"))
async def handler(message: Message):
...
def main():
broker = Broker(config={
"IN_TOPIC": "test-topic",
})
broker.include_router(router)
This way, we can make any static routing definition truly lazy, and we don't need to rely on global state. This also allows for more flexible configuration options. The approach should be the same for both subscribers and publishers.
The configuration can be used as a value for any option, including nested ones. This is especially useful for complex configurations. For example
@rabbit_broker.subscriber(
queue=RabbitQueue(name="test", routing_key=Config("ROUTING_KEY"))
exchange=Config("EXCHANGE_NAME")
)
async def handler(message: Message): ...