Skip to content

Commit 2e1f026

Browse files
Add FastAPI and Litestar server examples
1 parent 58b5706 commit 2e1f026

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

examples/server/asgi/README.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ fiddle.py
2828
This is a very simple application based on a JavaScript example of the same
2929
name.
3030

31+
fastapi-fiddle.py
32+
-----------------
33+
34+
A version of `fiddle.py` that is integrated with the FastAPI framework.
35+
36+
litestar-fiddle.py
37+
------------------
38+
39+
A version of `fiddle.py` that is integrated with the Litestar framework.
40+
3141
Running the Examples
3242
--------------------
3343

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python
2+
from fastapi import FastAPI
3+
from fastapi.responses import FileResponse
4+
from fastapi.staticfiles import StaticFiles
5+
import socketio
6+
import uvicorn
7+
8+
app = FastAPI()
9+
app.mount('/static', StaticFiles(directory='static'), name='static')
10+
11+
sio = socketio.AsyncServer(async_mode='asgi')
12+
combined_asgi_app = socketio.ASGIApp(sio, app)
13+
14+
15+
@app.get('/')
16+
async def index():
17+
return FileResponse('fiddle.html')
18+
19+
20+
@app.get('/hello')
21+
async def hello():
22+
return {'message': 'Hello, World!'}
23+
24+
25+
@sio.event
26+
async def connect(sid, environ, auth):
27+
print(f'connected auth={auth} sid={sid}')
28+
await sio.emit('hello', (1, 2, {'hello': 'you'}), to=sid)
29+
30+
31+
@sio.event
32+
def disconnect(sid):
33+
print('disconnected', sid)
34+
35+
36+
if __name__ == '__main__':
37+
uvicorn.run(combined_asgi_app, host='127.0.0.1', port=5000)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python
2+
from litestar import Litestar, get, MediaType
3+
from litestar.response import File
4+
from litestar.static_files.config import StaticFilesConfig
5+
import socketio
6+
import uvicorn
7+
8+
sio = socketio.AsyncServer(async_mode='asgi')
9+
10+
11+
@get('/', media_type=MediaType.HTML)
12+
async def index() -> File:
13+
return File('fiddle.html', content_disposition_type='inline')
14+
15+
16+
@get('/hello')
17+
async def hello() -> dict:
18+
return {'message': 'Hello, World!'}
19+
20+
21+
@sio.event
22+
async def connect(sid, environ, auth):
23+
print(f'connected auth={auth} sid={sid}')
24+
await sio.emit('hello', (1, 2, {'hello': 'you'}), to=sid)
25+
26+
27+
@sio.event
28+
def disconnect(sid):
29+
print('disconnected', sid)
30+
31+
32+
app = Litestar([index, hello], static_files_config=[
33+
StaticFilesConfig('static', directories=['static'])])
34+
combined_asgi_app = socketio.ASGIApp(sio, app)
35+
36+
37+
if __name__ == '__main__':
38+
uvicorn.run(combined_asgi_app, host='127.0.0.1', port=5000)

0 commit comments

Comments
 (0)