diff --git a/apps/maxkb/settings/base.py b/apps/maxkb/settings/base.py index 8be04fafd..8078f0b9c 100644 --- a/apps/maxkb/settings/base.py +++ b/apps/maxkb/settings/base.py @@ -63,8 +63,7 @@ REST_FRAMEWORK = { 'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema', 'DEFAULT_AUTHENTICATION_CLASSES': ['common.auth.authenticate.AnonymousAuthentication'] } -STATICFILES_DIRS = [(os.path.join(PROJECT_DIR, 'ui', 'dist'))] - +STATICFILES_DIRS = [(os.path.join(PROJECT_DIR, 'ui', 'dist')), (os.path.join(PROJECT_DIR, 'chat', 'dist'))] STATIC_ROOT = os.path.join(BASE_DIR.parent, 'static') ROOT_URLCONF = 'maxkb.urls' @@ -82,6 +81,19 @@ TEMPLATES = [ ], }, }, + {"NAME": "CHAT", + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': ["apps/static/chat"], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, ] SPECTACULAR_SETTINGS = { 'TITLE': 'MaxKB API', diff --git a/apps/maxkb/urls.py b/apps/maxkb/urls.py index 0e9992471..690fdb836 100644 --- a/apps/maxkb/urls.py +++ b/apps/maxkb/urls.py @@ -14,11 +14,17 @@ 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')) """ +import os + +from django.http import HttpResponse from django.urls import path, re_path, include from django.views import static from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView +from rest_framework import status +from common.result import Result from maxkb import settings +from maxkb.conf import PROJECT_DIR urlpatterns = [ path("api/", include("users.urls")), @@ -28,7 +34,7 @@ urlpatterns = [ path("api/", include("knowledge.urls")), path("api/", include("system_manage.urls")), path("api/", include("application.urls")), - path("chat/", include("chat.urls")), + path("chat/api/", include("chat.urls")), path('oss/', include('oss.urls')), ] urlpatterns += [ @@ -39,3 +45,52 @@ urlpatterns += [ urlpatterns.append( re_path(r'^static/(?P.*)$', static.serve, {'document_root': settings.STATIC_ROOT}, name='static'), ) + + +def pro(): + # 暴露静态主要是swagger资源 + urlpatterns.append( + re_path(r'^static/(?P.*)$', static.serve, {'document_root': settings.STATIC_ROOT}, name='static'), + ) + # 暴露ui静态资源 + urlpatterns.append( + re_path(r'^ui/(?P.*)$', static.serve, {'document_root': os.path.join(settings.STATIC_ROOT, "ui")}, + name='ui'), + ) + # 暴露ui静态资源 + urlpatterns.append( + re_path(r'^chat/(?P.*)$', static.serve, {'document_root': os.path.join(settings.STATIC_ROOT, "chat")}, + name='chat'), + ) + + +if not settings.DEBUG: + pro() + + +def get_index_html(index_path): + file = open(index_path, "r", encoding='utf-8') + content = file.read() + file.close() + return content + + +def page_not_found(request, exception): + """ + 页面不存在处理 + """ + if request.path.startswith("/api/"): + return Result(response_status=status.HTTP_404_NOT_FOUND, code=404, message="HTTP_404_NOT_FOUND") + if request.path.startswith("/chat/api/"): + return Result(response_status=status.HTTP_404_NOT_FOUND, code=404, message="HTTP_404_NOT_FOUND") + if request.path.startswith('/chat'): + index_path = os.path.join(PROJECT_DIR, 'apps', "static", 'chat', 'index.html') + else: + index_path = os.path.join(PROJECT_DIR, 'apps', "static", 'ui', 'index.html') + if not os.path.exists(index_path): + return HttpResponse("页面不存在", status=404) + content = get_index_html(index_path) + return HttpResponse(content, status=200) + + +handler404 = page_not_found