From 71943c0ab3d972fd8142555a07f8239c235d61da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20M=C3=A9taireau?= Date: Mon, 18 Mar 2024 11:42:05 +0100 Subject: [PATCH 1/5] chore: use asgi rather than wsgi By relying on Django Channels and Daphne. This requires a change in how the application is deployed to take that into account. --- pyproject.toml | 2 ++ umap/asgi.py | 16 ++++++++++++++++ umap/settings/base.py | 3 ++- 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 umap/asgi.py diff --git a/pyproject.toml b/pyproject.toml index 177c5f0c..e36038b5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,6 +28,8 @@ classifiers = [ "Programming Language :: Python :: 3.12", ] dependencies = [ + "channels==4.0.0", + "daphne==4.1.0", "Django==5.1.1", "django-agnocomplete==2.2.0", "django-environ==0.11.2", diff --git a/umap/asgi.py b/umap/asgi.py new file mode 100644 index 00000000..00487a9c --- /dev/null +++ b/umap/asgi.py @@ -0,0 +1,16 @@ +import os + +from channels.routing import ProtocolTypeRouter +from django.core.asgi import get_asgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "umap.settings") +# Initialize Django ASGI application early to ensure the AppRegistry +# is populated before importing code that may import ORM models. +django_asgi_app = get_asgi_application() + +application = ProtocolTypeRouter( + { + "http": django_asgi_app, + # Just HTTP for now. (We can add other protocols later.) + } +) diff --git a/umap/settings/base.py b/umap/settings/base.py index 73a1bf71..81131b9a 100644 --- a/umap/settings/base.py +++ b/umap/settings/base.py @@ -19,7 +19,7 @@ env = environ.Env() INTERNAL_IPS = env.list("INTERNAL_IPS", default=["127.0.0.1"]) ALLOWED_HOSTS = env.list("ALLOWED_HOSTS", default=["*"]) ADMINS = tuple(parseaddr(email) for email in env.list("ADMINS", default=[])) - +ASGI_APPLICATION = "umap.asgi.application" DEBUG = env.bool("DEBUG", default=False) @@ -119,6 +119,7 @@ LANGUAGES = ( SECRET_KEY = env("SECRET_KEY", default=None) INSTALLED_APPS = ( + "daphne", "django.contrib.auth", "django.contrib.contenttypes", "django.contrib.sessions", From aa246aaf05d1c81bcad8933544e2d3477c1a7881 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20M=C3=A9taireau?= Date: Thu, 21 Mar 2024 10:06:27 +0100 Subject: [PATCH 2/5] WIP: Add an async route for WS --- umap/asgi.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/umap/asgi.py b/umap/asgi.py index 00487a9c..f81d24d3 100644 --- a/umap/asgi.py +++ b/umap/asgi.py @@ -1,7 +1,10 @@ import os -from channels.routing import ProtocolTypeRouter +from channels.auth import AuthMiddlewareStack +from channels.routing import ProtocolTypeRouter, URLRouter +from channels.security.websocket import AllowedHostsOriginValidator from django.core.asgi import get_asgi_application +from django.urls import re_path os.environ.setdefault("DJANGO_SETTINGS_MODULE", "umap.settings") # Initialize Django ASGI application early to ensure the AppRegistry @@ -12,5 +15,14 @@ application = ProtocolTypeRouter( { "http": django_asgi_app, # Just HTTP for now. (We can add other protocols later.) + "websocket": AllowedHostsOriginValidator( + AuthMiddlewareStack( + URLRouter( + [ + re_path(r"^ws/$", consumers.AsyncChatConsumer.as_asgi()), + ] + ) + ) + ), } ) From b3e88b6096eed1d28d33c90d69836dd22c619ede Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Mon, 14 Oct 2024 12:10:13 +0200 Subject: [PATCH 3/5] chore: make daphne/asgi optional for now --- pyproject.toml | 6 ++++-- umap/asgi.py | 14 +------------- umap/settings/base.py | 1 - 3 files changed, 5 insertions(+), 16 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index e36038b5..8155bc55 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,8 +28,6 @@ classifiers = [ "Programming Language :: Python :: 3.12", ] dependencies = [ - "channels==4.0.0", - "daphne==4.1.0", "Django==5.1.1", "django-agnocomplete==2.2.0", "django-environ==0.11.2", @@ -69,6 +67,10 @@ test = [ docker = [ "uwsgi==2.0.27", ] +sync = [ + "channels==4.1.0", + "daphne==4.1.2", +] [project.scripts] umap = "umap.bin:main" diff --git a/umap/asgi.py b/umap/asgi.py index f81d24d3..00487a9c 100644 --- a/umap/asgi.py +++ b/umap/asgi.py @@ -1,10 +1,7 @@ import os -from channels.auth import AuthMiddlewareStack -from channels.routing import ProtocolTypeRouter, URLRouter -from channels.security.websocket import AllowedHostsOriginValidator +from channels.routing import ProtocolTypeRouter from django.core.asgi import get_asgi_application -from django.urls import re_path os.environ.setdefault("DJANGO_SETTINGS_MODULE", "umap.settings") # Initialize Django ASGI application early to ensure the AppRegistry @@ -15,14 +12,5 @@ application = ProtocolTypeRouter( { "http": django_asgi_app, # Just HTTP for now. (We can add other protocols later.) - "websocket": AllowedHostsOriginValidator( - AuthMiddlewareStack( - URLRouter( - [ - re_path(r"^ws/$", consumers.AsyncChatConsumer.as_asgi()), - ] - ) - ) - ), } ) diff --git a/umap/settings/base.py b/umap/settings/base.py index 81131b9a..c00ef253 100644 --- a/umap/settings/base.py +++ b/umap/settings/base.py @@ -119,7 +119,6 @@ LANGUAGES = ( SECRET_KEY = env("SECRET_KEY", default=None) INSTALLED_APPS = ( - "daphne", "django.contrib.auth", "django.contrib.contenttypes", "django.contrib.sessions", From c74971bc0ed4d58728bac84a3263f0fe301ba523 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Mon, 21 Oct 2024 11:50:10 +0200 Subject: [PATCH 4/5] chore: move pydantic and websocket in sync dependencies target --- pyproject.toml | 4 ++-- umap/asgi.py | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 8155bc55..1f10aadd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,13 +34,11 @@ dependencies = [ "django-probes==1.7.0", "Pillow==10.4.0", "psycopg==3.2.3", - "pydantic==2.9.2", "requests==2.32.3", "rcssmin==1.1.2", "rjsmin==1.2.2", "social-auth-core==4.5.4", "social-auth-app-django==5.4.2", - "websockets==13.1", ] [project.optional-dependencies] @@ -70,6 +68,8 @@ docker = [ sync = [ "channels==4.1.0", "daphne==4.1.2", + "pydantic==2.9.2", + "websockets==13.1", ] [project.scripts] diff --git a/umap/asgi.py b/umap/asgi.py index 00487a9c..2ca12ddc 100644 --- a/umap/asgi.py +++ b/umap/asgi.py @@ -11,6 +11,5 @@ django_asgi_app = get_asgi_application() application = ProtocolTypeRouter( { "http": django_asgi_app, - # Just HTTP for now. (We can add other protocols later.) } ) From 13aa407e47b52ecfdb49bb83ec4d235291ec2541 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Mon, 21 Oct 2024 11:59:58 +0200 Subject: [PATCH 5/5] chore: install sync target on "make develop" --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 9386033b..36b5110c 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ install: ## Install the dependencies .PHONY: develop develop: ## Install the test and dev dependencies - python3 -m pip install -e .[test,dev] + python3 -m pip install -e .[test,dev,sync] playwright install .PHONY: format