Added daphne support

This commit is contained in:
Alexander Karpov 2022-07-02 14:57:58 +03:00
parent e70360a1af
commit 4fd016f603
4 changed files with 32 additions and 5 deletions

View File

@ -16,7 +16,7 @@ $ docker run -p 6379:6379 -d redis:5
### run ### run
```shell ```shell
$ python3 manage.py runserver 0.0.0.0:8000 $ daphne -b 0.0.0.0 -p 8000 chess_backend.asgi:application
``` ```
### Описание команд сокетов ### Описание команд сокетов

View File

@ -11,10 +11,14 @@ DEBUG = True
ALLOWED_HOSTS = [] ALLOWED_HOSTS = []
if DEBUG:
ALLOWED_HOSTS = ["*"]
INSTALLED_APPS = [ INSTALLED_APPS = [
"django.contrib.sessions", "django.contrib.sessions",
"django.contrib.auth", "django.contrib.auth",
"django.contrib.contenttypes", "django.contrib.contenttypes",
"django.contrib.staticfiles",
"django.contrib.messages", "django.contrib.messages",
# Packages # Packages
"rest_framework", "rest_framework",
@ -25,10 +29,26 @@ INSTALLED_APPS = [
] ]
if DEBUG: if DEBUG:
INSTALLED_APPS.append("django.contrib.staticfiles")
INSTALLED_APPS.append("drf_yasg") INSTALLED_APPS.append("drf_yasg")
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
MIDDLEWARE = [ MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware", "django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware", "django.contrib.sessions.middleware.SessionMiddleware",

View File

@ -22,8 +22,10 @@ schema_view = get_schema_view(
permission_classes=(permissions.AllowAny,), permission_classes=(permissions.AllowAny,),
) )
urlpatterns = [path("api/", include("game.urls"))] + static( urlpatterns = (
settings.MEDIA_URL, document_root=settings.MEDIA_ROOT [path("api/", include("game.urls"))]
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
) )
if settings.DEBUG: if settings.DEBUG:
@ -43,4 +45,4 @@ if settings.DEBUG:
schema_view.with_ui("redoc", cache_timeout=0), schema_view.with_ui("redoc", cache_timeout=0),
name="schema-redoc", name="schema-redoc",
), ),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) ]

View File

@ -1,9 +1,14 @@
import json import json
import os
import django
from asgiref.sync import sync_to_async from asgiref.sync import sync_to_async
from channels.generic.websocket import AsyncWebsocketConsumer from channels.generic.websocket import AsyncWebsocketConsumer
from channels.layers import get_channel_layer from channels.layers import get_channel_layer
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "chess_backend.settings")
django.setup()
from game.models import Deck from game.models import Deck
from room.models import PlayerInQueue, Room, PlayerInRoom, GameState from room.models import PlayerInQueue, Room, PlayerInRoom, GameState
from room.services.room_create import create_room from room.services.room_create import create_room