Skip to content

Commit a01513a

Browse files
Sehat1137Lancetnikkumaranvpl
authored
docs: add information about manual AsyncAPI hosting (#2177)
* docs: add information about manual AsyncAPI hosting * docs: polish code examples * docs: add link to ASGI * Proofread docs --------- Co-authored-by: Sehat1137 <[email protected]> Co-authored-by: Pastukhov Nikita <[email protected]> Co-authored-by: Kumaran Rajendhiran <[email protected]>
1 parent bf3b244 commit a01513a

File tree

1 file changed

+97
-3
lines changed

1 file changed

+97
-3
lines changed

docs/docs/en/getting-started/asyncapi/hosting.md

Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ search:
1010

1111
# Serving the AsyncAPI Documentation
1212

13-
FastStream provides a command to serve the AsyncAPI documentation.
13+
## Using CLI and http.server
14+
15+
**FastStream** provides a command to serve the **AsyncAPI** documentation.
1416

1517
!!! note
1618
This feature requires an Internet connection to obtain the **AsyncAPI HTML** via **CDN**.
@@ -19,7 +21,7 @@ FastStream provides a command to serve the AsyncAPI documentation.
1921
{! docs_src/getting_started/asyncapi/serve.py [ln:17] !}
2022
```
2123

22-
In the above command, we are providing the path in the format of `python_module:FastStream`. Alternatively, you can also specify `asyncapi.json` or `asyncapi.yaml` to serve the AsyncAPI documentation.
24+
In the above command, the path is specified in the format of `python_module:FastStream`. Alternatively, you can also specify `asyncapi.json` or `asyncapi.yaml` to serve the **AsyncAPI** documentation.
2325

2426
=== "JSON"
2527
```shell
@@ -31,7 +33,7 @@ In the above command, we are providing the path in the format of `python_module:
3133
{!> docs_src/getting_started/asyncapi/serve.py [ln:25] !}
3234
```
3335

34-
After running the command, it should serve the AsyncAPI documentation on port **8000** and display the following logs in the terminal.
36+
After running the command, the **AsyncAPI** documentation will be served on port **8000**, and the terminal should display the following logs.
3537

3638
```{.shell .no-copy}
3739
INFO: Started server process [2364992]
@@ -52,6 +54,98 @@ And you should be able to see the following page in your browser:
5254
!!! tip
5355
The command also offers options to serve the documentation on a different host and port.
5456

57+
## Built-in ASGI for FastStream Applications
58+
59+
FastStream includes lightweight [**ASGI** support](../asgi.md){.internal-link} that you can use to serve both your application and the **AsyncAPI** documentation.
60+
61+
```python linenums="1"
62+
from faststream import FastStream
63+
from faststream.kafka import KafkaBroker
64+
65+
broker = KafkaBroker()
66+
67+
@broker.subscriber('topic')
68+
async def my_handler(msg: str) -> None:
69+
print(msg)
70+
71+
app = FastStream(broker).as_asgi(
72+
asyncapi_path="/docs/asyncapi",
73+
)
74+
75+
if __name__ == "__main__":
76+
import uvicorn
77+
uvicorn.run(app, host="0.0.0.0", port=8000)
78+
```
79+
80+
After running the script, the **AsyncAPI** docs will be available at: <http://localhost:8000/docs/asyncapi>
81+
82+
## Integration with Different HTTP Frameworks (**FastAPI** Example)
83+
84+
**FastStream** provides two robust approaches to combine your message broker documentation with any **ASGI** web frameworks.
85+
You can choose the method that best fits with your application architecture.
86+
87+
=== "Option 1"
88+
```python linenums="1" hl_lines="23-26"
89+
from typing import AsyncIterator
90+
from contextlib import asynccontextmanager
91+
92+
from fastapi import FastAPI, responses
93+
from faststream import FastStream
94+
from faststream.asyncapi import get_asyncapi_html, get_app_schema
95+
from faststream.kafka import KafkaBroker
96+
97+
broker = KafkaBroker()
98+
99+
@broker.subscriber('topic')
100+
async def my_handler(msg: str) -> None:
101+
print(msg)
102+
103+
@asynccontextmanager
104+
async def broker_lifespan(app: FastAPI) -> AsyncIterator[None]:
105+
async with broker:
106+
await broker.start()
107+
yield
108+
109+
app = FastAPI(lifespan=broker_lifespan)
110+
111+
@app.get('/docs/asyncapi')
112+
async def asyncapi() -> responses.HTMLResponse:
113+
schema = get_app_schema(FastStream(broker))
114+
return responses.HTMLResponse(get_asyncapi_html(schema))
115+
```
116+
117+
=== "Option 2"
118+
```python linenums="1" hl_lines="23"
119+
from typing import AsyncIterator
120+
from contextlib import asynccontextmanager
121+
122+
from fastapi import FastAPI
123+
from faststream import FastStream
124+
from faststream.asgi import make_asyncapi_asgi
125+
from faststream.kafka import KafkaBroker
126+
127+
broker = KafkaBroker()
128+
fs_app = FastStream(broker)
129+
130+
@broker.subscriber('topic')
131+
async def my_handler(msg: str) -> None:
132+
print(msg)
133+
134+
@asynccontextmanager
135+
async def broker_lifespan(app: FastAPI) -> AsyncIterator[None]:
136+
async with broker:
137+
await broker.start()
138+
yield
139+
140+
app = FastAPI(lifespan=broker_lifespan)
141+
app.mount("/docs/asyncapi", make_asyncapi_asgi(fs_app))
142+
```
143+
144+
After running the app, the documentation will be available at:
145+
146+
* OpenAPI Docs: <http://localhost:8000/docs>
147+
* AsyncAPI Docs: <http://localhost:8000/docs/asyncapi>
148+
55149
## Customizing AsyncAPI Documentation
56150

57151
FastStream also provides query parameters to show and hide specific sections of AsyncAPI documentation.

0 commit comments

Comments
 (0)