feat: web ui (#3257)

This commit is contained in:
shaohuzhang1 2025-06-13 17:35:25 +08:00 committed by GitHub
parent 6a37d22540
commit b6fb059512
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 70 additions and 3 deletions

View File

@ -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',

View File

@ -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<path>.*)$', static.serve, {'document_root': settings.STATIC_ROOT}, name='static'),
)
def pro():
# 暴露静态主要是swagger资源
urlpatterns.append(
re_path(r'^static/(?P<path>.*)$', static.serve, {'document_root': settings.STATIC_ROOT}, name='static'),
)
# 暴露ui静态资源
urlpatterns.append(
re_path(r'^ui/(?P<path>.*)$', static.serve, {'document_root': os.path.join(settings.STATIC_ROOT, "ui")},
name='ui'),
)
# 暴露ui静态资源
urlpatterns.append(
re_path(r'^chat/(?P<path>.*)$', 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