Much more flexible support for static files in the server · Mexarm/python-socketio@aaa87a8 · GitHub
Skip to content

Commit aaa87a8

Browse files
Much more flexible support for static files in the server
1 parent 3b32dbd commit aaa87a8

5 files changed

Lines changed: 95 additions & 45 deletions

File tree

docs/server.rst

Lines changed: 83 additions & 25 deletions

examples/server/asgi/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
sio = socketio.AsyncServer(async_mode='asgi')
99
app = socketio.ASGIApp(sio, static_files={
10-
'/': {'content_type': 'text/html', 'filename': 'app.html'},
10+
'/': 'app.html',
1111
})
1212
background_task_started = False
1313

examples/server/asgi/latency.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55

66
sio = socketio.AsyncServer(async_mode='asgi')
77
app = socketio.ASGIApp(sio, static_files={
8-
'/': {'content_type': 'text/html', 'filename': 'latency.html'},
9-
'/static/style.css': {'content_type': 'text/css',
10-
'filename': 'static/style.css'},
8+
'/': 'latency.html',
9+
'/static': 'static',
1110
})
1211

1312

socketio/asgi.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ class ASGIApp(engineio.ASGIApp): # pragma: no cover
1010
1111
:param socketio_server: The Socket.IO server. Must be an instance of the
1212
``socketio.AsyncServer`` class.
13-
:param static_files: A dictionary where the keys are URLs that should be
14-
served as static files. For each URL, the value is
15-
a dictionary with ``content_type`` and ``filename``
16-
keys. This option is intended to be used for serving
17-
client files during development.
13+
:param static_files: A dictionary with static file mapping rules. See the
14+
documentation for details on this argument.
1815
:param other_asgi_app: A separate ASGI app that receives all other traffic.
1916
:param socketio_path: The endpoint where the Socket.IO application should
2017
be installed. The default value is appropriate for
@@ -25,13 +22,12 @@ class ASGIApp(engineio.ASGIApp): # pragma: no cover
2522
import socketio
2623
import uvicorn
2724
28-
eio = socketio.AsyncServer()
29-
app = engineio.ASGIApp(eio, static_files={
30-
'/': {'content_type': 'text/html', 'filename': 'index.html'},
31-
'/index.html': {'content_type': 'text/html',
32-
'filename': 'index.html'},
25+
sio = socketio.AsyncServer()
26+
app = engineio.ASGIApp(sio, static_files={
27+
'/': 'index.html',
28+
'/static': './public',
3329
})
34-
uvicorn.run(app, '127.0.0.1', 5000)
30+
uvicorn.run(app, host='127.0.0.1', port=5000)
3531
"""
3632
def __init__(self, socketio_server, other_asgi_app=None,
3733
static_files=None, socketio_path='socket.io'):

socketio/middleware.py

Lines changed: 2 additions & 5 deletions

0 commit comments

Comments
 (0)