Skip to content

Commit 2ad5883

Browse files
authored
feat: dynamic URL (#3444)
1 parent 9b89e8f commit 2ad5883

File tree

15 files changed

+106
-45
lines changed

15 files changed

+106
-45
lines changed

apps/maxkb/conf.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,15 @@ def get_log_level(self):
102102
return self.get('LOG_LEVEL', 'DEBUG')
103103

104104
def get_sandbox_python_package_paths(self):
105-
return self.get('SANDBOX_PYTHON_PACKAGE_PATHS', '/opt/py3/lib/python3.11/site-packages,/opt/maxkb-app/sandbox/python-packages,/opt/maxkb/python-packages')
105+
return self.get('SANDBOX_PYTHON_PACKAGE_PATHS',
106+
'/opt/py3/lib/python3.11/site-packages,/opt/maxkb-app/sandbox/python-packages,/opt/maxkb/python-packages')
106107

107108
def get_admin_path(self):
108109
return self.get('ADMIN_PATH', 'admin')
109110

111+
def get_chat_path(self):
112+
return self.get('CHAT_PATH', '/chat')
113+
110114
def get_session_timeout(self):
111115
return int(self.get('SESSION_TIMEOUT', 28800))
112116

apps/maxkb/settings/base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,23 @@
5555
'django.middleware.security.SecurityMiddleware',
5656
'django.contrib.sessions.middleware.SessionMiddleware',
5757
'django.middleware.common.CommonMiddleware',
58-
'django.contrib.messages.middleware.MessageMiddleware',
58+
5959
]
6060

6161
REST_FRAMEWORK = {
6262
'EXCEPTION_HANDLER': 'common.exception.handle_exception.handle_exception',
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')), (os.path.join(PROJECT_DIR, 'chat', 'dist'))]
66+
STATICFILES_DIRS = [(os.path.join(PROJECT_DIR, 'ui', 'dist'))]
6767
STATIC_ROOT = os.path.join(BASE_DIR.parent, 'static')
6868
ROOT_URLCONF = 'maxkb.urls'
6969
APPS_DIR = os.path.join(PROJECT_DIR, 'apps')
7070

7171
TEMPLATES = [
7272
{
7373
'BACKEND': 'django.template.backends.django.DjangoTemplates',
74-
'DIRS': ["apps/static/ui"],
74+
'DIRS': ["apps/static/admin"],
7575
'APP_DIRS': True,
7676
'OPTIONS': {
7777
'context_processors': [

apps/maxkb/urls.py

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"""
1717
import os
1818

19-
from django.http import HttpResponse
19+
from django.http import HttpResponse,HttpResponseRedirect
2020
from django.urls import path, re_path, include
2121
from django.views import static
2222
from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView
@@ -25,16 +25,21 @@
2525
from common.result import Result
2626
from maxkb import settings
2727
from maxkb.conf import PROJECT_DIR
28+
from maxkb.const import CONFIG
2829

30+
admin_api_prefix = CONFIG.get_admin_path()[1:] + '/api/'
31+
admin_ui_prefix = CONFIG.get_admin_path()
32+
chat_api_prefix = CONFIG.get_chat_path()[1:] + '/api/'
33+
chat_ui_prefix = CONFIG.get_chat_path()
2934
urlpatterns = [
30-
path("api/", include("users.urls")),
31-
path("api/", include("tools.urls")),
32-
path("api/", include("models_provider.urls")),
33-
path("api/", include("folders.urls")),
34-
path("api/", include("knowledge.urls")),
35-
path("api/", include("system_manage.urls")),
36-
path("api/", include("application.urls")),
37-
path("chat/api/", include("chat.urls")),
35+
path(admin_api_prefix, include("users.urls")),
36+
path(admin_api_prefix, include("tools.urls")),
37+
path(admin_api_prefix, include("models_provider.urls")),
38+
path(admin_api_prefix, include("folders.urls")),
39+
path(admin_api_prefix, include("knowledge.urls")),
40+
path(admin_api_prefix, include("system_manage.urls")),
41+
path(admin_api_prefix, include("application.urls")),
42+
path(chat_api_prefix, include("chat.urls")),
3843
path('oss/', include('oss.urls')),
3944
]
4045
urlpatterns += [
@@ -54,12 +59,14 @@ def pro():
5459
)
5560
# 暴露ui静态资源
5661
urlpatterns.append(
57-
re_path(r'^ui/(?P<path>.*)$', static.serve, {'document_root': os.path.join(settings.STATIC_ROOT, "ui")},
58-
name='ui'),
62+
re_path(rf"^{CONFIG.get_admin_path()[1:]}/(?P<path>.*)$", static.serve,
63+
{'document_root': os.path.join(settings.STATIC_ROOT, "admin")},
64+
name='admin'),
5965
)
6066
# 暴露ui静态资源
6167
urlpatterns.append(
62-
re_path(r'^chat/(?P<path>.*)$', static.serve, {'document_root': os.path.join(settings.STATIC_ROOT, "chat")},
68+
re_path(rf'^{CONFIG.get_chat_path()[1:]}/(?P<path>.*)$', static.serve,
69+
{'document_root': os.path.join(settings.STATIC_ROOT, "chat")},
6370
name='chat'),
6471
)
6572

@@ -79,18 +86,26 @@ def page_not_found(request, exception):
7986
"""
8087
页面不存在处理
8188
"""
82-
if request.path.startswith("/api/"):
89+
if request.path.startswith(admin_ui_prefix + '/api/'):
8390
return Result(response_status=status.HTTP_404_NOT_FOUND, code=404, message="HTTP_404_NOT_FOUND")
84-
if request.path.startswith("/chat/api/"):
91+
if request.path.startswith(chat_ui_prefix + '/api/'):
8592
return Result(response_status=status.HTTP_404_NOT_FOUND, code=404, message="HTTP_404_NOT_FOUND")
86-
if request.path.startswith('/chat'):
93+
if request.path.startswith(chat_ui_prefix):
8794
index_path = os.path.join(PROJECT_DIR, 'apps', "static", 'chat', 'index.html')
95+
content = get_index_html(index_path)
96+
content.replace("prefix: '/chat'", f"prefix: {CONFIG.get_chat_path()}")
97+
if not os.path.exists(index_path):
98+
return HttpResponse("页面不存在", status=404)
99+
return HttpResponse(content, status=200)
100+
elif request.path.startswith(admin_ui_prefix):
101+
index_path = os.path.join(PROJECT_DIR, 'apps', "static", 'admin', 'index.html')
102+
if not os.path.exists(index_path):
103+
return HttpResponse("页面不存在", status=404)
104+
content = get_index_html(index_path)
105+
content = content.replace("prefix: '/admin'", f"prefix: '{CONFIG.get_admin_path()}'")
106+
return HttpResponse(content, status=200)
88107
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)
108+
return HttpResponseRedirect(admin_ui_prefix+'/')
94109

95110

96111
handler404 = page_not_found
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
<base target="_blank" />
88
<title>%VITE_APP_TITLE%</title>
99
</head>
10+
<script>
11+
window.MaxKB = {
12+
prefix: '/admin',
13+
}
14+
</script>
1015
<body>
1116
<div id="app"></div>
1217
<script type="module" src="/src/main.ts"></script>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
<base target="_blank" />
88
<title>%VITE_APP_TITLE%</title>
99
</head>
10+
<script>
11+
window.MaxKB = {
12+
prefix: '/chat',
13+
}
14+
</script>
1015
<body>
1116
<div id="app"></div>
1217
<script type="module" src="/src/chat.ts"></script>

ui/env.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
/// <reference types="vite/client" />
2+
interface Window {
3+
sendMessage: ?((message: string, other_params_data: any) => void)
4+
MaxKB: {
5+
prefix: string
6+
}
7+
}

ui/env/.env

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
VITE_APP_NAME=ui
2-
VITE_BASE_PATH=/ui/
1+
VITE_APP_NAME=admin
2+
VITE_BASE_PATH=/admin/
33
VITE_APP_PORT=3000
44
VITE_APP_TITLE = 'MaxKB'
5-
VITE_ENTRY="entry/system/index.html"
5+
VITE_ENTRY="admin.html"

ui/env/.env.chat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ VITE_APP_NAME=chat
22
VITE_BASE_PATH=/chat/
33
VITE_APP_PORT=3001
44
VITE_APP_TITLE = 'MaxKB'
5-
VITE_ENTRY="entry/chat/index.html"
5+
VITE_ENTRY="chat.html"

ui/src/api/application/application.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ const open: (application_id: string, loading?: Ref<boolean>) => Promise<Result<s
177177
* data
178178
*/
179179
const chat: (chat_id: string, data: any) => Promise<any> = (chat_id, data) => {
180-
return postStream(`/api/chat_message/${chat_id}`, data)
180+
const prefix = (window.MaxKB?.prefix ? window.MaxKB?.prefix : '/admin') + '/api'
181+
return postStream(`${prefix}/chat_message/${chat_id}`, data)
181182
}
182183
/**
183184
* 获取对话用户认证类型

ui/src/api/chat/chat.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ const open: (loading?: Ref<boolean>) => Promise<Result<string>> = (loading) => {
4040
* data
4141
*/
4242
const chat: (chat_id: string, data: any) => Promise<any> = (chat_id, data) => {
43-
return postStream(`/chat/api/chat_message/${chat_id}`, data)
43+
const prefix = (window.MaxKB?.prefix ? window.MaxKB?.prefix : '/chat') + '/api'
44+
return postStream(`${prefix}/chat_message/${chat_id}`, data)
4445
}
4546

4647
/**

0 commit comments

Comments
 (0)