Skip to content

Commit

Permalink
Update serve asgi app example
Browse files Browse the repository at this point in the history
  • Loading branch information
oeway committed Sep 3, 2024
1 parent 520e8c1 commit 5ff9bf4
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions docs/asgi-apps.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ The following example shows how to use FastAPI to create a web application in th

You need to first install fastapi:

In Pyodide, you can install it using micropip:

```python
# This assumes you are in a Pyodide environment, if not, you can install fastapi using `pip install fastapi==0.70.0`
import micropip
await micropip.install(["fastapi==0.70.0"])
```

Or in normal Python, you can install it using pip:

```bash
pip install -U fastapi
```

Then you can create a FastAPI app and serve it through Hypha:

```python
Expand Down Expand Up @@ -47,9 +54,11 @@ async def main():
async def test():
return {"message": "Hello, it works!", "server_url": server_url}

async def serve_fastapi(args):
async def serve_fastapi(args, context=None):
# context can be used for do authorization
# e.g. check user id against a list of allowed users
scope = args["scope"]
print(f'{scope["client"]} - {scope["method"]} - {scope["path"]}')
print(f'{context["user"]["id"]} - {scope["client"]} - {scope["method"]} - {scope["path"]}')
await app(args["scope"], args["receive"], args["send"])

svc_info = await server.register_service({
Expand All @@ -58,7 +67,8 @@ async def main():
"type": "ASGI",
"serve": serve_fastapi,
"config":{
"visibility": "public"
"visibility": "public",
"require_context": True
}
})

Expand Down

0 comments on commit 5ff9bf4

Please sign in to comment.