Add FastStream example to docs#958
Conversation
| :description: This example demonstrates a usage of FastStream with Dependency Injector. | ||
|
|
||
|
|
||
| This example shows how to use ``Dependency Injector`` with `FastStream <https://dummy.faststream.airt.ai/0.5/faststream/>`_. |
There was a problem hiding this comment.
| This example shows how to use ``Dependency Injector`` with `FastStream <https://dummy.faststream.airt.ai/0.5/faststream/>`_. | |
| This example shows how to use ``Dependency Injector`` with `FastStream <https://github.com/ag2ai/faststream>`_. |
| FastStream example | ||
| ============= |
There was a problem hiding this comment.
| FastStream example | |
| ============= | |
| FastStream example | |
| ================== |
| counter = providers.Singleton(Counter) | ||
|
|
||
|
|
||
| broker = RedisBroker("redis://redis", logger=None) |
There was a problem hiding this comment.
| counter = providers.Singleton(Counter) | |
| broker = RedisBroker("redis://redis", logger=None) | |
| config = providers.Configuration() | |
| counter = providers.Singleton(Counter) | |
| broker = providers.Resource(RedisBroker, config.redis_url, logger=None) | |
| app = providers.Factory(FastStream, broker, logger=None) |
If we're demonstrating integration, it would be better to go all-out.
|
|
||
| await FastStream(broker, logger=None).run() |
There was a problem hiding this comment.
see comment above
| await FastStream(broker, logger=None).run() | |
| app = await container.app() | |
| await app.run() |
|
|
||
|
|
||
| def main(): | ||
| client = redis.Redis(host="redis", port=6379) |
| Ensure that ``run.sh`` has execution permission: | ||
|
|
||
| .. code-block:: bash | ||
|
|
||
| sudo chmod +x ./run.sh |
There was a problem hiding this comment.
| Ensure that ``run.sh`` has execution permission: | |
| .. code-block:: bash | |
| sudo chmod +x ./run.sh |
You can chmod +x ./run.sh and commit it right away. Git keeps track of mode flags.
| consumer: | ||
| container_name: "${COMPOSE_PROJECT_NAME}-consumer" | ||
| build: | ||
| dockerfile: Dockerfile.consumer | ||
| depends_on: | ||
| - redis | ||
|
|
||
| producer: | ||
| container_name: "${COMPOSE_PROJECT_NAME}-producer" | ||
| build: | ||
| dockerfile: Dockerfile.producer |
There was a problem hiding this comment.
| consumer: | |
| container_name: "${COMPOSE_PROJECT_NAME}-consumer" | |
| build: | |
| dockerfile: Dockerfile.consumer | |
| depends_on: | |
| - redis | |
| producer: | |
| container_name: "${COMPOSE_PROJECT_NAME}-producer" | |
| build: | |
| dockerfile: Dockerfile.producer | |
| consumer: | |
| image: faststream-example | |
| depends_on: | |
| - redis | |
| producer: | |
| image: faststream-example |
See comment about extra Dockerfile.
There was a problem hiding this comment.
Add reference to docs/examples/index.rst so it appears alongside other examples.
| docker container rm \ | ||
| faststream-example-producer \ | ||
| faststream-example-consumer \ | ||
| faststream-example-redis | ||
|
|
||
| docker image rm \ | ||
| faststream-example-producer \ | ||
| faststream-example-consumer |
There was a problem hiding this comment.
| docker container rm \ | |
| faststream-example-producer \ | |
| faststream-example-consumer \ | |
| faststream-example-redis | |
| docker image rm \ | |
| faststream-example-producer \ | |
| faststream-example-consumer | |
| docker compose down --rmi local |
Though, I'm not sure if this is a desired behavior, since user might want to tinker with an example app for longer. Better include it in the documentation.
| @@ -0,0 +1,15 @@ | |||
| #!/usr/bin/env bash | |||
|
|
|||
Despite
FastStreamusesFastDependsunder the hood the existing example forFastDependsfrom documentation doesn't work out of the box when used inFastStreamhandlers.So I thought that having an explicit example of how to use
dependency_injectorwithFastStreamwould be helpful for other devs seeking for a solution for such integration.