1616"""
1717import os
1818
19- from django .http import HttpResponse
19+ from django .http import HttpResponse , HttpResponseRedirect
2020from django .urls import path , re_path , include
2121from django .views import static
2222from drf_spectacular .views import SpectacularAPIView , SpectacularRedocView , SpectacularSwaggerView
2525from common .result import Result
2626from maxkb import settings
2727from 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 ()
2934urlpatterns = [
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]
4045urlpatterns += [
@@ -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
96111handler404 = page_not_found
0 commit comments