Prechádzať zdrojové kódy

修复: 当post和put方式请求接口时候找不到方法的问题

root 3 rokov pred
rodič
commit
3f8291a074

+ 1 - 1
monitor/monitor/middleware.py

@@ -13,7 +13,7 @@ class ExceptionGlobeMiddleware(MiddlewareMixin):
         '''视图函数调用之后,response返回浏览器之前'''
         status_code = response.status_code
         path = request.path
-        if path.startswith('/api/schema'):
+        if path.startswith('/api/schema') or path.startswith('/doc/'):
             return response
 
         result_data = {

+ 2 - 35
monitor/monitor/settings.py

@@ -55,15 +55,14 @@ INSTALLED_APPS = [
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'rest_framework',
+    'drf_yasg',
     'drf_spectacular',
-    'corsheaders',
     'monitor_app'
 ]
 
 MIDDLEWARE = [
     'django.middleware.security.SecurityMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',
-    'corsheaders.middleware.CorsMiddleware',
     'django.middleware.common.CommonMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
@@ -75,8 +74,6 @@ MIDDLEWARE = [
 REST_FRAMEWORK = {
     # YOUR SETTINGS
     'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
-    # 全局配置异常模块
-    # 'EXCEPTION_HANDLER': 'monitor.exception.globe_exception_handler',
     'DEFAULT_PAGINATION_CLASS': 'monitor.pagination.CustomPagination',
 }
 
@@ -144,34 +141,4 @@ USE_TZ = True
 # Static files (CSS, JavaScript, Images)
 # https://docs.djangoproject.com/en/2.2/howto/static-files/
 
-STATIC_URL = '/static/'
-
-CORS_ALLOW_CREDENTIALS = True
-CORS_ORIGIN_ALLOW_ALL = True
-
-# CORS_ORIGIN_WHITELIST = (
-#     'http://*'
-# )
-
-CORS_ALLOW_METHODS = (
-    'DELETE',
-    'GET',
-    'OPTIONS',
-    'PATCH',
-    'POST',
-    'PUT'
-)
-
-CORS_ALLOW_HEADERS = (
-    'XMLHttpRequest',
-    'X_FILENAME',
-    'accept-encoding',
-    'authorization',
-    'content-type',
-    'dnt',
-    'origin',
-    'user-agent',
-    'x-csrftoken',
-    'x-requested-with',
-    'Pragma',
-)
+STATIC_URL = '/static/'

+ 43 - 1
monitor/monitor/urls.py

@@ -13,10 +13,49 @@ Including another URLconf
     1. Import the include() function: from django.urls import include, path
     2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
 """
+from rest_framework import permissions
 from django.contrib import admin
-from django.urls import path, include
+from django.urls import path, include, re_path
 from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView
 
+from drf_yasg import openapi
+from drf_yasg.views import get_schema_view
+from drf_yasg.inspectors import CoreAPICompatInspector, FieldInspector, NotHandled, SwaggerAutoSchema
+
+schema_view = get_schema_view(
+    openapi.Info(
+        title="云飞监控API文档",
+        default_version="v1",
+        description="""
+<strong>HTTP状态码</strong>:
+    200 表示成功
+    400 表示请求失败
+    403 表示无权限访问
+    500 表示服务器异
+
+<strong>接口返回格式统一为</strong>:
+
+{
+&nbsp;&nbsp; "msg": "", &nbsp;&nbsp; 错误描述信息,正常响应下此为空字符串,只有错误异常情况下才有内容
+&nbsp;&nbsp; "code": 0, &nbsp;&nbsp; 响应状态码:0表示正确,400表示请求错误,403表示无权限访问,500表示服务器异常。
+&nbsp;&nbsp; "result": {
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "data": {}, &nbsp;&nbsp; 后端返回的数据, 格式为对象或者数组,没有数据返回为空对象
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "paging": {&nbsp;&nbsp;&nbsp;&nbsp;# 分页信息,如果没有分页,则为空对象
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'next': 1,	&nbsp;&nbsp;&nbsp;&nbsp;# 下一页页码
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'previous': 1, &nbsp;&nbsp;&nbsp;&nbsp;# 上一页页码
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'total': 10, &nbsp;&nbsp;&nbsp;&nbsp;# 总页数
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'page': 1, &nbsp;&nbsp;&nbsp;&nbsp;# 当前页
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'page_size': 5, &nbsp;&nbsp;&nbsp;&nbsp;# 每页条目数
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'total_page': 10 &nbsp;&nbsp;&nbsp;&nbsp;# 总页数
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
+&nbsp;&nbsp;&nbsp;&nbsp;}
+}
+"""
+    ),
+    public=True,
+    permission_classes=(permissions.AllowAny, )
+)
+
 
 api_urlpatterns = [
     path('', include('monitor_app.urls'))
@@ -29,4 +68,7 @@ urlpatterns = [
     path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
     path('api/schema/swagger-ui/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
     path('api/schema/redoc/', SpectacularRedocView.as_view(url_name='schema'), name='redoc'),
+
+    re_path(r'doc/swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
+    re_path(r'doc/redoc/$', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
 ]

+ 7 - 1
monitor/utils/supervisord_utils.py

@@ -25,7 +25,7 @@ class SupervisordUtils:
             result.append(info)
         return result
 
-    def restart(self, name):
+    def stop(self, name):
         try:
             info = self.supervisor.getProcessInfo(name)
             statename = info['statename']
@@ -38,6 +38,12 @@ class SupervisordUtils:
         self.supervisor.reloadConfig()
         self.supervisor.addProcessGroup(name)
 
+    def restart(self, name):
+        self.stop(name)
+        self.supervisor.removeProcessGroup(name)
+        self.supervisor.reloadConfig()
+        self.supervisor.addProcessGroup(name)
+
     def remove(self, name):
         try:
             info = self.supervisor.getProcessInfo(name)