Sfoglia il codice sorgente

maint: change `platform.aiohttp.static_routes()`'s signature

wangweimin 4 anni fa
parent
commit
effb32ea11

+ 1 - 2
docs/guide.rst

@@ -505,12 +505,11 @@ PyWebIO 目前支持与Flask、Tornado、Django和aiohttp Web框架的集成。
 
             from aiohttp import web
             from pywebio.platform.aiohttp import static_routes, webio_handler
-            from pywebio import STATIC_PATH
 
             app = web.Application()
             # task_func 为使用PyWebIO编写的任务函数
             app.add_routes([web.get('/io', webio_handler(task_func))])  # http通信接口
-            app.add_routes(static_routes(STATIC_PATH))  # 前端静态文件托管
+            app.add_routes(static_routes('/'))  # 前端静态文件托管
 
             web.run_app(app, host='localhost', port=8080)
 

+ 10 - 6
pywebio/platform/aiohttp.py

@@ -136,16 +136,20 @@ def webio_handler(applications, allowed_origins=None, check_origin=None, websock
                           websocket_settings=websocket_settings)
 
 
-def static_routes(static_path):
-    """获取用于提供PyWebIO静态文件的aiohttp路由"""
+def static_routes(prefix='/'):
+    """获取用于提供PyWebIO静态文件的aiohttp路由列表
+
+    :param str prefix: 静态文件托管的URL路径,默认为根路径 ``/``
+    :return: aiohttp路由列表
+    """
 
     async def index(request):
         return web.FileResponse(path.join(STATIC_PATH, 'index.html'))
 
-    files = [path.join(static_path, d) for d in listdir(static_path)]
+    files = [path.join(STATIC_PATH, d) for d in listdir(STATIC_PATH)]
     dirs = filter(path.isdir, files)
-    routes = [web.static('/' + path.basename(d), d) for d in dirs]
-    routes.append(web.get('/', index))
+    routes = [web.static(prefix + path.basename(d), d) for d in dirs]
+    routes.append(web.get(prefix, index))
     return routes
 
 
@@ -191,7 +195,7 @@ def start_server(applications, port=0, host='', debug=False,
 
     app = web.Application(**aiohttp_settings)
     app.router.add_routes([web.get('/io', handler)])
-    app.router.add_routes(static_routes(STATIC_PATH))
+    app.router.add_routes(static_routes())
 
     if auto_open_webbrowser:
         asyncio.get_event_loop().create_task(open_webbrowser_on_server_started('localhost', port))

+ 1 - 1
test/10.aiohttp_multiple_session_impliment.py

@@ -53,7 +53,7 @@ def start_test_server():
     app = web.Application()
     app.add_routes([web.get('/io', webio_handler(target))])
     app.add_routes([web.get('/io2', webio_handler(async_target))])
-    app.add_routes(static_routes(STATIC_PATH))
+    app.add_routes(static_routes())
 
     web.run_app(app, host='127.0.0.1', port=8080)