Skip to content

Commit

Permalink
Update runserver for Asgi v3 (#1523)
Browse files Browse the repository at this point in the history
* Convert StaticFilesWrapper to single callable.
* Make ProtocolTypeRouter instantiate default AsgiHandler.
* Adapt runserver command to ASGI v3.
  • Loading branch information
carltongibson committed Oct 21, 2020
1 parent eb552dd commit 841d170
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
3 changes: 2 additions & 1 deletion channels/management/commands/runserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import sys

from daphne.cli import ASGI3Middleware
from daphne.endpoints import build_endpoint_description_strings
from daphne.server import Server
from django.apps import apps
Expand Down Expand Up @@ -104,7 +105,7 @@ def inner_run(self, *args, **options):
endpoints = build_endpoint_description_strings(host=self.addr, port=self.port)
try:
self.server_cls(
application=self.get_application(options),
application=ASGI3Middleware(self.get_application(options)),
endpoints=endpoints,
signal_handlers=not options["use_reloader"],
action_logger=self.log_action,
Expand Down
2 changes: 1 addition & 1 deletion channels/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ProtocolTypeRouter:
def __init__(self, application_mapping):
self.application_mapping = application_mapping
if "http" not in self.application_mapping:
self.application_mapping["http"] = AsgiHandler
self.application_mapping["http"] = AsgiHandler()

async def __call__(self, scope, receive, send):
if scope["type"] in self.application_mapping:
Expand Down
10 changes: 7 additions & 3 deletions channels/staticfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,24 @@ def _should_handle(self, path):
"""
return path.startswith(self.base_url[2]) and not self.base_url[1]

def __call__(self, scope):
def __call__(self, scope, receive, send):
# Only even look at HTTP requests
if scope["type"] == "http" and self._should_handle(scope["path"]):
# Serve static content
return StaticFilesHandler(dict(scope, static_base_url=self.base_url))
return StaticFilesHandler()(
dict(scope, static_base_url=self.base_url), receive, send
)
# Hand off to the main app
return self.application(scope)
return self.application(scope, receive, send)


class StaticFilesHandler(AsgiHandler):
"""
Subclass of AsgiHandler that serves directly from its get_response.
"""

# TODO: Review hierarchy here. Do we NEED to inherit BaseHandler, AsgiHandler?

def file_path(self, url):
"""
Returns the relative path to the media file on disk for the given URL.
Expand Down
2 changes: 1 addition & 1 deletion tests/test_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async def test_protocol_type_router():
assert await router({"type": "http"}, None, None) == "http"
# Test defaulting to AsgiHandler
router = ProtocolTypeRouter({"websocket": MockApplication(return_value="ws")})
assert router.application_mapping["http"] == AsgiHandler
assert isinstance(router.application_mapping["http"], AsgiHandler)
# Test an unmatched type
with pytest.raises(ValueError):
await router({"type": "aprs"}, None, None)
Expand Down

0 comments on commit 841d170

Please sign in to comment.