Skip to content

Commit 5a0624a

Browse files
committed
feat: web ui
1 parent 67ec3c1 commit 5a0624a

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

apps/maxkb/settings/base.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@
6363
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
6464
'DEFAULT_AUTHENTICATION_CLASSES': ['common.auth.authenticate.AnonymousAuthentication']
6565
}
66-
STATICFILES_DIRS = [(os.path.join(PROJECT_DIR, 'ui', 'dist'))]
67-
66+
STATICFILES_DIRS = [(os.path.join(PROJECT_DIR, 'ui', 'dist')), (os.path.join(PROJECT_DIR, 'chat', 'dist'))]
6867
STATIC_ROOT = os.path.join(BASE_DIR.parent, 'static')
6968
ROOT_URLCONF = 'maxkb.urls'
7069

@@ -82,6 +81,19 @@
8281
],
8382
},
8483
},
84+
{"NAME": "CHAT",
85+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
86+
'DIRS': ["apps/static/chat"],
87+
'APP_DIRS': True,
88+
'OPTIONS': {
89+
'context_processors': [
90+
'django.template.context_processors.debug',
91+
'django.template.context_processors.request',
92+
'django.contrib.auth.context_processors.auth',
93+
'django.contrib.messages.context_processors.messages',
94+
],
95+
},
96+
},
8597
]
8698
SPECTACULAR_SETTINGS = {
8799
'TITLE': 'MaxKB API',

apps/maxkb/urls.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@
1414
1. Import the include() function: from django.urls import include, path
1515
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
1616
"""
17+
import os
18+
19+
from django.http import HttpResponse
1720
from django.urls import path, re_path, include
1821
from django.views import static
1922
from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView
23+
from rest_framework import status
2024

25+
from common.result import Result
2126
from maxkb import settings
27+
from maxkb.conf import PROJECT_DIR
2228

2329
urlpatterns = [
2430
path("api/", include("users.urls")),
@@ -28,7 +34,7 @@
2834
path("api/", include("knowledge.urls")),
2935
path("api/", include("system_manage.urls")),
3036
path("api/", include("application.urls")),
31-
path("chat/", include("chat.urls")),
37+
path("chat/api/", include("chat.urls")),
3238
path('oss/', include('oss.urls')),
3339
]
3440
urlpatterns += [
@@ -39,3 +45,52 @@
3945
urlpatterns.append(
4046
re_path(r'^static/(?P<path>.*)$', static.serve, {'document_root': settings.STATIC_ROOT}, name='static'),
4147
)
48+
49+
50+
def pro():
51+
# 暴露静态主要是swagger资源
52+
urlpatterns.append(
53+
re_path(r'^static/(?P<path>.*)$', static.serve, {'document_root': settings.STATIC_ROOT}, name='static'),
54+
)
55+
# 暴露ui静态资源
56+
urlpatterns.append(
57+
re_path(r'^ui/(?P<path>.*)$', static.serve, {'document_root': os.path.join(settings.STATIC_ROOT, "ui")},
58+
name='ui'),
59+
)
60+
# 暴露ui静态资源
61+
urlpatterns.append(
62+
re_path(r'^chat/(?P<path>.*)$', static.serve, {'document_root': os.path.join(settings.STATIC_ROOT, "chat")},
63+
name='chat'),
64+
)
65+
66+
67+
if not settings.DEBUG:
68+
pro()
69+
70+
71+
def get_index_html(index_path):
72+
file = open(index_path, "r", encoding='utf-8')
73+
content = file.read()
74+
file.close()
75+
return content
76+
77+
78+
def page_not_found(request, exception):
79+
"""
80+
页面不存在处理
81+
"""
82+
if request.path.startswith("/api/"):
83+
return Result(response_status=status.HTTP_404_NOT_FOUND, code=404, message="HTTP_404_NOT_FOUND")
84+
if request.path.startswith("/chat/api/"):
85+
return Result(response_status=status.HTTP_404_NOT_FOUND, code=404, message="HTTP_404_NOT_FOUND")
86+
if request.path.startswith('/chat'):
87+
index_path = os.path.join(PROJECT_DIR, 'apps', "static", 'chat', 'index.html')
88+
else:
89+
index_path = os.path.join(PROJECT_DIR, 'apps', "static", 'ui', 'index.html')
90+
if not os.path.exists(index_path):
91+
return HttpResponse("页面不存在", status=404)
92+
content = get_index_html(index_path)
93+
return HttpResponse(content, status=200)
94+
95+
96+
handler404 = page_not_found

0 commit comments

Comments
 (0)