From 4531100393aa4c94700edd5df657968c5fc9affc Mon Sep 17 00:00:00 2001 From: RogerWork Date: Mon, 19 Feb 2024 13:43:03 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E8=AF=95=20uvicorn?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dingxin_toolbox_drf/asgi.py | 18 +++++------------- dingxin_toolbox_drf/routing.py | 13 +++++++++++++ dingxin_toolbox_drf/settings.py | 6 ++++-- dingxin_toolbox_drf/urls.py | 8 ++++---- 4 files changed, 26 insertions(+), 19 deletions(-) create mode 100644 dingxin_toolbox_drf/routing.py diff --git a/dingxin_toolbox_drf/asgi.py b/dingxin_toolbox_drf/asgi.py index 7f20245..e67a3f0 100644 --- a/dingxin_toolbox_drf/asgi.py +++ b/dingxin_toolbox_drf/asgi.py @@ -9,23 +9,15 @@ https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/ import os import django -from django.conf import settings -from django.core.asgi import get_asgi_application -from channels.routing import ProtocolTypeRouter, URLRouter +# from django.conf import settings +# # from django.core.asgi import get_asgi_application +# from channels.routing import ProtocolTypeRouter, URLRouter from channels.routing import get_default_application -from .urls import websocket_urlpatterns + from .wsgi import * os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dingxin_toolbox_drf.settings') # os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings') -# application = get_asgi_application() -application = ProtocolTypeRouter( - { - 'http': get_asgi_application(), - # 'http': get_default_application(), - 'websocket': URLRouter(websocket_urlpatterns) - } -) -settings.configure(DEBUG=True) django.setup() +application = get_default_application() diff --git a/dingxin_toolbox_drf/routing.py b/dingxin_toolbox_drf/routing.py new file mode 100644 index 0000000..14e7df7 --- /dev/null +++ b/dingxin_toolbox_drf/routing.py @@ -0,0 +1,13 @@ +from .wsgi import * +# from channels.auth import AuthMiddlewareStack +from channels.routing import ProtocolTypeRouter, URLRouter +from django.core.asgi import get_asgi_application +from .urls import websocket_urlpatterns + + +application = ProtocolTypeRouter( + { + 'http': get_asgi_application(), + 'websocket': URLRouter(websocket_urlpatterns) + } +) \ No newline at end of file diff --git a/dingxin_toolbox_drf/settings.py b/dingxin_toolbox_drf/settings.py index 541b71b..c153716 100644 --- a/dingxin_toolbox_drf/settings.py +++ b/dingxin_toolbox_drf/settings.py @@ -31,6 +31,8 @@ SECRET_KEY = 'django-insecure-hfujy!ih1uvio_p^cfdj^%8juf_wdb@szu$t=aaacd)^(zf@b% # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True +# 增加异步和同步多线程的支持 +os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true" ALLOWED_HOSTS = ['*'] @@ -42,11 +44,11 @@ INSTALLED_APPS = [ 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', - 'rest_framework', 'channels', # 'daphne', 'django.contrib.staticfiles', 'corsheaders', # coreapi生成接口文档 + 'rest_framework', 'rest_framework.authtoken', # Token验证应用 'django_filters', 'update', @@ -85,7 +87,7 @@ TEMPLATES = [ WSGI_APPLICATION = 'dingxin_toolbox_drf.wsgi.application' # 设置ASGI -ASGI_APPLICATION = 'dingxin_toolbox_drf.asgi.application' +ASGI_APPLICATION = 'dingxin_toolbox_drf.routing.application' # Database # https://docs.djangoproject.com/en/4.2/ref/settings/#databases diff --git a/dingxin_toolbox_drf/urls.py b/dingxin_toolbox_drf/urls.py index a7c32f9..b5a6aa1 100644 --- a/dingxin_toolbox_drf/urls.py +++ b/dingxin_toolbox_drf/urls.py @@ -16,16 +16,16 @@ Including another URLconf """ from django.contrib import admin from django.urls import path, include -# from rest_framework.documentation import include_docs_urls -# from rest_framework.authtoken.views import obtain_auth_token +from rest_framework.documentation import include_docs_urls +from rest_framework.authtoken.views import obtain_auth_token from update import consumers urlpatterns = [ path('admin/', admin.site.urls), - # path('api-token-auth/', obtain_auth_token), + path('api-token-auth/', obtain_auth_token), path('update/', include('update.urls')), # cinema Update 备注 path('', include('mock.urls')), - # path('docs/', include_docs_urls(title='接口文档')), + path('docs/', include_docs_urls(title='接口文档')), ] websocket_urlpatterns = [