-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from kpn/47-update-examples
47 update examples
- Loading branch information
Showing
13 changed files
with
77 additions
and
62 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Empty file.
10 changes: 0 additions & 10 deletions
10
examples/fastapi-example/fastapi_example/streaming/streams.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from fastapi import FastAPI | ||
from starlette_prometheus import PrometheusMiddleware, metrics | ||
|
||
from .resources import stream_engine | ||
from .streams import consume | ||
from .views import router | ||
|
||
app = FastAPI() | ||
|
||
|
||
@app.on_event("startup") | ||
async def startup_event(): | ||
await stream_engine.start() | ||
|
||
|
||
@app.on_event("shutdown") | ||
async def shutdown_event(): | ||
await stream_engine.stop() | ||
|
||
|
||
stream_engine.add_stream(consume) | ||
app.include_router(router) | ||
app.add_middleware(PrometheusMiddleware, filter_unhandled_paths=True) | ||
app.add_api_route("/metrics", metrics) # type: ignore |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from kstreams import Stream, stream | ||
|
||
|
||
@stream("local--kstream") | ||
async def consume(stream: Stream): | ||
print("consuming.....") | ||
async for cr in stream: | ||
print(f"Event consumed: headers: {cr.headers}, payload: {cr.value}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from fastapi import APIRouter | ||
from fastapi.responses import JSONResponse | ||
|
||
from .resources import stream_engine | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.get("/events") | ||
async def produce_event(): | ||
"""Send an event to the cluster. | ||
This should be a POST, but like this it can be | ||
easily executed in the browser. | ||
""" | ||
payload = '{"message": "hello world!"}' | ||
|
||
metadata = await stream_engine.send( | ||
"local--kstream", | ||
value=payload.encode(), | ||
) | ||
|
||
msg = { | ||
"topic": metadata.topic, | ||
"partition": metadata.partition, | ||
"offset": metadata.offset, | ||
} | ||
return JSONResponse(content=msg) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[tool.poetry] | ||
name = "fastapi-example" | ||
name = "fastapi_webapp" | ||
version = "0.1.0" | ||
description = "" | ||
authors = ["Marcos Schroh <[email protected]>"] | ||
|
@@ -14,3 +14,6 @@ uvicorn = "^0.18.2" | |
[build-system] | ||
requires = ["poetry-core>=1.0.0"] | ||
build-backend = "poetry.core.masonry.api" | ||
|
||
[tool.poetry.scripts] | ||
app = "fastapi_webapp.__main__:main" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters