Skip to content

Commit 6588a97

Browse files
authored
Add example for medium/large projects in FastAPI (#595)
1 parent 3812158 commit 6588a97

File tree

14 files changed

+124
-0
lines changed

14 files changed

+124
-0
lines changed

examples/fastapi/__init__.py

Whitespace-only changes.

examples/fastapi/api/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from fastapi import APIRouter
2+
3+
from api.my_api import router as my_api_router
4+
5+
router = APIRouter()
6+
7+
router.include_router(my_api_router)

examples/fastapi/api/my_api.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import Union
2+
from fastapi import APIRouter
3+
4+
from my_faust.table.my_table import greetings_table
5+
6+
router = APIRouter()
7+
8+
9+
@router.get("/items/{item_id}")
10+
def read_item(item_id: int, q: Union[str, None] = None):
11+
return {"item_id": item_id, "q": q}
12+
13+
14+
@router.get("/table")
15+
def read_table():
16+
return [{k: v} for k, v in greetings_table.items()]

examples/fastapi/main.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from contextlib import asynccontextmanager
2+
from fastapi import FastAPI
3+
from api import router as api_router
4+
5+
from my_faust.timer import router as timer_router
6+
from my_faust.app import faust_app
7+
8+
9+
# This is just hello_world.py integrated with a FastAPI application
10+
11+
12+
def fake_answer_to_everything_ml_model(x: float):
13+
return x * 42
14+
15+
16+
ml_models = {}
17+
18+
19+
@asynccontextmanager
20+
async def lifespan(app: FastAPI):
21+
faust_app.discover()
22+
await faust_app.start()
23+
yield
24+
await faust_app.stop()
25+
26+
27+
# You MUST have "app" defined in order for Faust to discover the app
28+
# if you're using "faust" on CLI, but this doesn't work yet
29+
app = fastapi_app = FastAPI(
30+
lifespan=lifespan,
31+
)
32+
33+
# For now, run via "uvicorn fastapi_example:app"
34+
# then visit http://127.0.0.1:8000/docs
35+
36+
app.include_router(router=api_router)
37+
app.include_router(router=timer_router)
38+
39+
40+
@app.get("/")
41+
def read_root():
42+
return {"Hello": "World"}

examples/fastapi/my_faust/__init__.py

Whitespace-only changes.

examples/fastapi/my_faust/agent/__init__.py

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from my_faust.app import faust_app
2+
from my_faust.table.my_table import greetings_table
3+
from my_faust.topic.my_topic import greetings_topic
4+
5+
6+
@faust_app.agent(greetings_topic)
7+
async def print_greetings(greetings):
8+
async for greeting in greetings:
9+
print(f"greeting: {greeting}")
10+
greetings_table[greeting] = {"hello world"}

examples/fastapi/my_faust/app.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import faust
2+
3+
4+
def get_all_packages_to_scan():
5+
return ["my_faust"]
6+
7+
8+
# You MUST have "app" defined in order for Faust to discover the app
9+
# if you're using "faust" on CLI, but this doesn't work yet
10+
# autodiscover https://faust-streaming.github.io/faust/userguide/settings.html#autodiscover
11+
app = faust_app = faust.App(
12+
'hello-world-fastapi',
13+
broker='kafka://localhost:9092',
14+
web_enabled=False,
15+
autodiscover=get_all_packages_to_scan,
16+
)
17+
18+
# For now, run via "faust -A my_faust.app worker -l info"

examples/fastapi/my_faust/table/__init__.py

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from my_faust.app import faust_app
2+
3+
greetings_table = faust_app.GlobalTable(
4+
name="greetings_table",
5+
default=dict,
6+
partitions=1,
7+
recovery_buffer_size=1,
8+
)

0 commit comments

Comments
 (0)