add ws
This commit is contained in:
16
api/asgi.py
16
api/asgi.py
@@ -9,8 +9,20 @@ https://docs.djangoproject.com/en/5.1/howto/deployment/asgi/
|
||||
|
||||
import os
|
||||
|
||||
from channels.routing import ProtocolTypeRouter, URLRouter
|
||||
from channels.auth import AuthMiddlewareStack
|
||||
from channels.security.websocket import AllowedHostsOriginValidator
|
||||
|
||||
from django.core.asgi import get_asgi_application
|
||||
from chat.url import websocket_urlpatterns
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api.settings')
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "api.settings")
|
||||
|
||||
application = get_asgi_application()
|
||||
application = ProtocolTypeRouter(
|
||||
{
|
||||
"http": get_asgi_application(),
|
||||
"websocket": AllowedHostsOriginValidator(
|
||||
AuthMiddlewareStack(URLRouter(websocket_urlpatterns))
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
@@ -41,6 +41,8 @@ else:
|
||||
# Application definition
|
||||
|
||||
INSTALLED_APPS = [
|
||||
"daphne",
|
||||
"channels",
|
||||
"django.contrib.admin",
|
||||
"django.contrib.auth",
|
||||
"django.contrib.contenttypes",
|
||||
@@ -53,6 +55,7 @@ INSTALLED_APPS = [
|
||||
"account",
|
||||
"task",
|
||||
"submission",
|
||||
"chat",
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
@@ -85,7 +88,7 @@ TEMPLATES = [
|
||||
]
|
||||
|
||||
WSGI_APPLICATION = "api.wsgi.application"
|
||||
|
||||
ASGI_APPLICATION = "api.asgi.application"
|
||||
|
||||
# Database
|
||||
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases
|
||||
@@ -129,6 +132,16 @@ else:
|
||||
# 配置缓存
|
||||
CACHES = PROD_CACHES
|
||||
|
||||
# WebSocket 的缓存
|
||||
CHANNEL_LAYERS = {
|
||||
"default": {
|
||||
"BACKEND": "channels_redis.core.RedisChannelLayer",
|
||||
"CONFIG": {
|
||||
"hosts": [(os.getenv("REDIS_HOST"), 6379)],
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
# Password validation
|
||||
# https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators
|
||||
|
||||
|
||||
@@ -34,4 +34,4 @@ apis = [
|
||||
path("api/", api.urls),
|
||||
]
|
||||
|
||||
urlpatterns = apis + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
||||
urlpatterns = apis + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
||||
Reference in New Issue
Block a user