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

View File

@ -1,5 +1,5 @@
"""passbook sentry integration""" """passbook sentry integration"""
from aioredis.errors import ReplyError from aioredis.errors import ReplyError, ConnectionClosedError
from billiard.exceptions import WorkerLostError from billiard.exceptions import WorkerLostError
from botocore.client import ClientError from botocore.client import ClientError
from celery.exceptions import CeleryError from celery.exceptions import CeleryError
@ -40,6 +40,7 @@ def before_send(event, hint):
RedisError, RedisError,
ResponseError, ResponseError,
ReplyError, ReplyError,
ConnectionClosedError,
# websocket errors # websocket errors
ChannelFull, ChannelFull,
WebSocketException, 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 For more information on this file, see
https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/ https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/
""" """
import os
import typing import typing
from time import time from time import time
from typing import Any, ByteString, Dict from typing import Any, ByteString, Dict
import django import django
from asgiref.compatibility import guarantee_single_callable from asgiref.compatibility import guarantee_single_callable
from channels.routing import ProtocolTypeRouter, URLRouter
from defusedxml import defuse_stdlib from defusedxml import defuse_stdlib
from django.core.asgi import get_asgi_application from django.core.asgi import get_asgi_application
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
from structlog import get_logger 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() defuse_stdlib()
django.setup() django.setup()
@ -129,5 +131,14 @@ class ASGILogger:
application = 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 = { CHANNEL_LAYERS = {
"default": { "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())]