diff --git a/docker-compose.yml b/docker-compose.yml index f4e3e07..6e96cbb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,3 +1,4 @@ +version: '3.8' services: db: @@ -13,6 +14,7 @@ services: web: build: . + command: daphne -b 0.0.0.0 -p 8000 testapi.asgi:application ports: - "8000:8000" volumes: diff --git a/nginx/nginx.conf b/nginx/nginx.conf index 0454044..ae5e444 100644 --- a/nginx/nginx.conf +++ b/nginx/nginx.conf @@ -1,34 +1,42 @@ -upstream django { - server web:8000; -} +events {} -server { - listen 80; - server_name localhost; - - # Static files - location /static/ { - alias /app/static/; - expires 1y; - access_log off; - add_header Cache-Control "public"; - gzip_static on; +http { + upstream django { + server web:8000; } - # Media files - location /media/ { - alias /app/media/; - expires 7d; - access_log off; - add_header Cache-Control "public"; - } + server { + listen 80; + server_name localhost; - # Django app - location / { - proxy_pass http://django; - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto $scheme; + # Static files + location /static/ { + alias /app/static/; + expires 1y; + access_log off; + add_header Cache-Control "public"; + gzip_static on; + } + + # Media files + location /media/ { + alias /app/media/; + expires 7d; + access_log off; + add_header Cache-Control "public"; + } + + # Django app + location / { + proxy_pass http://django; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + } } } \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 688620c..c861fc7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ requests django-cors-headers Pillow python-dotenv +channels>=4.0 \ No newline at end of file diff --git a/testapi/asgi.py b/testapi/asgi.py index b3f8740..a9995fa 100644 --- a/testapi/asgi.py +++ b/testapi/asgi.py @@ -8,9 +8,21 @@ https://docs.djangoproject.com/en/5.1/howto/deployment/asgi/ """ import os - +from channels.routing import ProtocolTypeRouter, URLRouter +from channels.auth import AuthMiddlewareStack from django.core.asgi import get_asgi_application +from .routing import websocket_urlpatterns + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'testapi.settings') -application = get_asgi_application() +django_asgi_app = get_asgi_application() + +application = ProtocolTypeRouter({ + "http": django_asgi_app, + "websocket": AuthMiddlewareStack( + URLRouter( + websocket_urlpatterns + ) + ), +}) diff --git a/testapi/consumers.py b/testapi/consumers.py new file mode 100644 index 0000000..f6f3570 --- /dev/null +++ b/testapi/consumers.py @@ -0,0 +1,14 @@ + +from channels.generic.websocket import AsyncWebsocketConsumer +import json + +class EchoConsumer(AsyncWebsocketConsumer): + async def connect(self): + await self.accept() + await self.send(text_data=json.dumps({"message": "WebSocket connected!"})) + + async def disconnect(self, close_code): + pass + + async def receive(self, text_data): + await self.send(text_data=json.dumps({"echo": text_data})) \ No newline at end of file diff --git a/testapi/routing.py b/testapi/routing.py new file mode 100644 index 0000000..911ee2b --- /dev/null +++ b/testapi/routing.py @@ -0,0 +1,8 @@ +# routing.py + +from django.urls import path +from . import consumers + +websocket_urlpatterns = [ + path("ws/echo/", consumers.EchoConsumer.as_asgi()), +] diff --git a/testapi/settings.py b/testapi/settings.py index cd6c31d..8dbeac2 100644 --- a/testapi/settings.py +++ b/testapi/settings.py @@ -99,7 +99,7 @@ TEMPLATES = [ ] WSGI_APPLICATION = 'testapi.wsgi.application' - +ASGI_APPLICATION = 'testapi.asgi.application' # Database # https://docs.djangoproject.com/en/5.1/ref/settings/#databases