operations_project/daren_project/wsgi.py

33 lines
1.0 KiB
Python
Raw Normal View History

2025-05-07 18:01:48 +08:00
"""
WSGI config for daren_project project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/5.2/howto/deployment/wsgi/
"""
import os
2025-05-13 11:58:17 +08:00
import django
2025-05-07 18:01:48 +08:00
2025-05-13 11:58:17 +08:00
# 首先设置 Django 设置模块
2025-05-07 18:01:48 +08:00
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'daren_project.settings')
2025-05-13 11:58:17 +08:00
django.setup() # 添加这行来初始化 Django
# 然后再导入其他模块
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
from channels.security.websocket import AllowedHostsOriginValidator
from apps.chat.routing import websocket_urlpatterns
from apps.common.middlewares import TokenAuthMiddleware
2025-05-07 18:01:48 +08:00
2025-05-13 11:58:17 +08:00
# 使用TokenAuthMiddleware代替AuthMiddlewareStack
application = ProtocolTypeRouter({
"http": get_asgi_application(),
"websocket": AllowedHostsOriginValidator(
TokenAuthMiddleware(
URLRouter(websocket_urlpatterns)
)
),
})