A quick POC for integrating FastAPI and Faust.
This project implements a simple incrementer web app. See the .gif
file below.
- We use Docker Compose to set up 4 containers:
- api
- worker
- kafka
- zookeeper
- The
worker
container is based on a custom docker image that has both Python and RocksDB installed already. To see how RocksDB is used in Faust, see here. - In the
worker
container, we define a GlobalTable to hold the current count for the incrementer. - Both the
worker
andapi
containers each have their own Faust app instance. However, theapi
Faust instance is started in "client-only" mode (i.e. just a producer and simple reply consumer). - When the user visits the webpage in their browser, the following happens:
- The browser makes a
GET
request to the API. - The API asks a Faust agent to retrieve the current incrementer count value.
- Once the API gets this value, it responds to the browser with HTML that includes the current count.
- The browser makes a
- When the user clicks the
Increment
button in their browser, the following happens:- A
POST
request is sent to the API. - The API then asks a Faust agent to increment the count value.
- Once the value is incremented, the API responds to the browser with a "redirect" response.
- The browser then redirects back to the original page, which displays the updated count value.
- A
- Install Docker on your machine.
git clone
this repo.cd
into this repo, and rundocker-compose up --build
.- Open your web browser, and visit http://localhost:8000/
- To stop running the app, go into the terminal, and press
Ctrl + C
. Then, rundocker-compose down
.