root: fix websockets not working correctly

This commit is contained in:
Jens Langhammer 2020-11-11 14:48:19 +01:00
parent c5eff4bdd6
commit 287cb72d6f
6 changed files with 26 additions and 19 deletions

View File

@ -1,4 +1,5 @@
"""Gunicorn config"""
import os
import warnings
from multiprocessing import cpu_count
from pathlib import Path
@ -14,6 +15,8 @@ worker_class = "uvicorn.workers.UvicornWorker"
# Docker containers don't have /tmp as tmpfs
worker_tmp_dir = "/dev/shm"
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "passbook.root.settings")
logconfig_dict = {
"version": 1,
"disable_existing_loggers": False,

View File

@ -1,5 +1,5 @@
"""passbook sentry integration"""
from aioredis.errors import ReplyError
from aioredis.errors import ReplyError, ConnectionClosedError
from billiard.exceptions import WorkerLostError
from botocore.client import ClientError
from celery.exceptions import CeleryError
@ -40,6 +40,7 @@ def before_send(event, hint):
RedisError,
ResponseError,
ReplyError,
ConnectionClosedError,
# websocket errors
ChannelFull,
WebSocketException,

View File

@ -6,19 +6,21 @@ It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/
"""
import os
import typing
from time import time
from typing import Any, ByteString, Dict
import django
from asgiref.compatibility import guarantee_single_callable
from channels.routing import ProtocolTypeRouter, URLRouter
from defusedxml import defuse_stdlib
from django.core.asgi import get_asgi_application
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
from structlog import get_logger
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "passbook.root.settings")
from passbook.root import websocket
# DJANGO_SETTINGS_MODULE is set in gunicorn.conf.py
defuse_stdlib()
django.setup()
@ -129,5 +131,14 @@ class ASGILogger:
application = ASGILogger(
guarantee_single_callable(SentryAsgiMiddleware(get_asgi_application()))
guarantee_single_callable(
SentryAsgiMiddleware(
ProtocolTypeRouter(
{
"http": get_asgi_application(),
"websocket": URLRouter(websocket.websocket_urlpatterns),
}
)
)
)
)

View File

@ -1,14 +0,0 @@
"""root Websocket URLS"""
from channels.routing import ProtocolTypeRouter, URLRouter
from django.urls import path
from passbook.outposts.channels import OutpostConsumer
application = ProtocolTypeRouter(
{
# (http->django views is added by default)
"websocket": URLRouter(
[path("ws/outpost/<uuid:pk>/", OutpostConsumer.as_asgi())]
),
}
)

View File

@ -208,7 +208,7 @@ TEMPLATES = [
},
]
ASGI_APPLICATION = "passbook.root.routing.application"
ASGI_APPLICATION = "passbook.root.asgi.application"
CHANNEL_LAYERS = {
"default": {

View File

@ -0,0 +1,6 @@
"""root Websocket URLS"""
from django.urls import path
from passbook.outposts.channels import OutpostConsumer
websocket_urlpatterns = [path("ws/outpost/<uuid:pk>/", OutpostConsumer.as_asgi())]