From 019a0017766e0ddb5c4ffe6c3f5c9eea84d16133 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=B9=E5=89=8D=E6=98=8E?= <909355014@qq.com> Date: Mon, 16 May 2022 15:50:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0swagger?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/server/settings.py | 6 ++++++ server/server/urls.py | 21 ++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/server/server/settings.py b/server/server/settings.py index b67a84b..1fe03be 100644 --- a/server/server/settings.py +++ b/server/server/settings.py @@ -136,6 +136,12 @@ STATICFILES_DIRS = ( MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media') +# swagger配置 +SWAGGER_SETTINGS = { + 'LOGIN_URL':'/api/admin/login/', + 'LOGOUT_URL':'/api/admin/logout/' +} + # restframework配置 REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': [ diff --git a/server/server/urls.py b/server/server/urls.py index 3140f0b..7a4d726 100644 --- a/server/server/urls.py +++ b/server/server/urls.py @@ -31,11 +31,26 @@ from django.conf.urls import url from rest_framework_simplejwt.serializers import TokenObtainPairSerializer from rest_framework_simplejwt.views import TokenViewBase from apps.system.views import WXMPlogin,mediaauth +from drf_yasg import openapi +from drf_yasg.views import get_schema_view +import os +schema_dict = dict( + info=openapi.Info( + title="CMA API", + default_version='v1', + contact=openapi.Contact(email="caoqianming@foxmail.com"), + license=openapi.License(name="MIT License"), + ), + public=True, + permission_classes=[],) +if os.getenv('DJANGO_SETTINGS_MODULE') != 'server.settings_dev': + schema_dict['url'] = "https://testsearch.ctc.ac.cn/" + +schema_view = get_schema_view(**schema_dict) urlpatterns = [ - path('', TemplateView.as_view(template_name="index.html")), path('api/admin/', admin.site.urls), path('api/mediaauth/',mediaauth), path('api/wxmplogin/',WXMPlogin.as_view()), @@ -51,5 +66,9 @@ urlpatterns = [ path('api/consulting/', include('apps.consulting.urls')), path('api/docs/', include_docs_urls(title="接口文档",authentication_classes=[], permission_classes=[])), + url(r'^api/swagger(?P\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'), + path('api/swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'), + path('api/redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'), path('api/', include(router.urls)), + path('', TemplateView.as_view(template_name="index.html")), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)