def test_mount_extra_static_files_dirs(tmpdir_factory): static_dir = tmpdir_factory.mktemp("staticfiles") _create_asset(static_dir) api = API(static_dir=None) api.mount("assets", static(str(static_dir))) response = api.client.get(f"/assets/{FILE_DIR}/{FILE_NAME}") assert response.status_code == 200 assert response.text == FILE_CONTENTS
def test_mount_extra_static_files_dirs(raw_app, tmpdir_factory): static_dir = tmpdir_factory.mktemp("staticfiles") _create_asset(static_dir) app = configure(raw_app, static_dir=None) app.mount("assets", static(str(static_dir))) client = create_client(app) response = client.get(f"/assets/{FILE_DIR}/{FILE_NAME}") assert response.status_code == 200 assert response.text == FILE_CONTENTS
# Create an ASGI-compliant app out of the socket.io server, # and mount it under the root app. # NOTE: "socket.io" is the default for `socketio_path`. We only add it # here for the sake of being explicit. # As a result, the client can connect at `/sio/socket.io`. app.mount("/sio", socketio.ASGIApp(sio, socketio_path="socket.io")) # Server static files for the socket.io client. # See: https://github.com/socketio/socket.io-client # NOTE: alternatively, the socket.io client could be served from # a CDN if you don't have npm/Node.js available in your runtime. # If so, static files would be linked to in the HTML page, and we wouldn't # need this line. # See: https://socket.io/docs/#Javascript-Client app.mount("/socket.io", static("node_modules/socket.io-client/dist")) @app.route("/") async def index(req, res): res.html = await templates.render("index.html") # Event handler for the "message" event. # See: https://python-socketio.readthedocs.io/en/latest/server.html#defining-event-handlers @sio.on("message") async def handle_message(sid, data: str): print("message:", data) # Broadcast the received message to all connected clients. # See: https://python-socketio.readthedocs.io/en/latest/server.html#emitting-events await sio.emit("response", data)
"""Application definition.""" from bocadillo import App, discover_providers, Templates, static app = App() discover_providers("chatbot.providerconf") templates = Templates(app, directory='dist') app.mount(prefix='js', app=static('dist/js')) app.mount(prefix='css', app=static('dist/css')) # Create routes here. @app.route('/') async def index(req, res): res.html = await templates.render("index.html") @app.websocket_route("/conversation") async def converse(ws, diego, save_client): async for message in ws: response = diego.get_response(message) await ws.send(str(response)) @app.route("/client-count") async def client_count(req, res, clients): res.json = {"count": len(clients)}