Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 51 additions & 26 deletions apps/maxkb/urls/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""

import os
import re
from pathlib import Path

from django.http import HttpResponse, HttpResponseRedirect
Expand All @@ -29,9 +31,9 @@
from maxkb.conf import PROJECT_DIR
from maxkb.const import CONFIG

admin_api_prefix = CONFIG.get_admin_path()[1:] + '/api/'
admin_api_prefix = CONFIG.get_admin_path()[1:] + "/api/"
admin_ui_prefix = CONFIG.get_admin_path()
chat_api_prefix = CONFIG.get_chat_path()[1:] + '/api/'
chat_api_prefix = CONFIG.get_chat_path()[1:] + "/api/"
chat_ui_prefix = CONFIG.get_chat_path()
urlpatterns = [
path(admin_api_prefix, include("users.urls")),
Expand All @@ -47,33 +49,47 @@
path(admin_api_prefix, include("portal.urls")),
path(chat_api_prefix, include("oss.urls", namespace="chat_oss")),
path(chat_api_prefix, include("chat.urls")),
path(f'{admin_ui_prefix[1:]}/', include('oss.retrieval_urls', namespace='admin_oss_retrieval')),
path(f'{chat_ui_prefix[1:]}/', include('oss.retrieval_urls', namespace='chat_oss_retrieval')),
path(f"{admin_ui_prefix[1:]}/", include("oss.retrieval_urls", namespace="admin_oss_retrieval")),
path(f"{chat_ui_prefix[1:]}/", include("oss.retrieval_urls", namespace="chat_oss_retrieval")),
]
init_doc(urlpatterns, chat_urlpatterns)


def pro():
urlpatterns.append(
re_path(rf'^{CONFIG.get_admin_path()[1:]}/api-doc/(?P<path>.*)$', static.serve,
{'document_root': os.path.join(settings.STATIC_ROOT, "drf_spectacular_sidecar")}, name='doc'),
re_path(
rf"^{CONFIG.get_admin_path()[1:]}/api-doc/(?P<path>.*)$",
static.serve,
{"document_root": os.path.join(settings.STATIC_ROOT, "drf_spectacular_sidecar")},
name="doc",
),
)

urlpatterns.append(
re_path(rf'^{CONFIG.get_chat_path()[1:]}/api-doc/(?P<path>.*)$', static.serve,
{'document_root': os.path.join(settings.STATIC_ROOT, "drf_spectacular_sidecar")}, name='doc_chat'),
re_path(
rf"^{CONFIG.get_chat_path()[1:]}/api-doc/(?P<path>.*)$",
static.serve,
{"document_root": os.path.join(settings.STATIC_ROOT, "drf_spectacular_sidecar")},
name="doc_chat",
),
)
# 暴露ui静态资源
urlpatterns.append(
re_path(rf"^{CONFIG.get_admin_path()[1:]}/(?P<path>.*)$", static.serve,
{'document_root': os.path.join(settings.STATIC_ROOT, "admin")},
name='admin'),
re_path(
rf"^{CONFIG.get_admin_path()[1:]}/(?P<path>.*)$",
static.serve,
{"document_root": os.path.join(settings.STATIC_ROOT, "admin")},
name="admin",
),
)
# 暴露ui静态资源
urlpatterns.append(
re_path(rf'^{CONFIG.get_chat_path()[1:]}/(?P<path>.*)$', static.serve,
{'document_root': os.path.join(settings.STATIC_ROOT, "chat")},
name='chat'),
re_path(
rf"^{CONFIG.get_chat_path()[1:]}/(?P<path>.*)$",
static.serve,
{"document_root": os.path.join(settings.STATIC_ROOT, "chat")},
name="chat",
),
)


Expand All @@ -82,44 +98,51 @@ def pro():


def get_index_html(index_path):
file = open(index_path, "r", encoding='utf-8')
file = open(index_path, "r", encoding="utf-8")
content = file.read()
file.close()
return content


def to_absolute_asset_path(content, ui_prefix):
"""
前端以 base './' 打包,深层路由下相对地址会解析到错误目录,
入口模块被以不同 URL 二次执行并重复挂载导致白屏,这里按当前配置的前缀改为绝对地址
"""
return re.sub(r'(\s(?:src|href)=")\./', rf"\g<1>{ui_prefix}/", content)


def get_all_files(directory):
base_path = Path(directory)
file_paths = [
'/' + str(file.relative_to(base_path)).replace('\\', '/')
for file in base_path.rglob('*')
if file.is_file()
"/" + str(file.relative_to(base_path)).replace("\\", "/") for file in base_path.rglob("*") if file.is_file()
]
return sorted(file_paths, key=len, reverse=True)


static_dict = {
chat_ui_prefix: get_all_files(os.path.join(PROJECT_DIR, 'apps', "static", 'chat')),
admin_ui_prefix: get_all_files(os.path.join(PROJECT_DIR, 'apps', "static", 'admin'))
chat_ui_prefix: get_all_files(os.path.join(PROJECT_DIR, "apps", "static", "chat")),
admin_ui_prefix: get_all_files(os.path.join(PROJECT_DIR, "apps", "static", "admin")),
}


def page_not_found(request, exception):
"""
页面不存在处理
"""
if request.path.startswith(admin_ui_prefix + '/api/'):
if request.path.startswith(admin_ui_prefix + "/api/"):
return Result(response_status=status.HTTP_404_NOT_FOUND, code=404, message="HTTP_404_NOT_FOUND")
if request.path.startswith(chat_ui_prefix + '/api/'):
if request.path.startswith(chat_ui_prefix + "/api/"):
return Result(response_status=status.HTTP_404_NOT_FOUND, code=404, message="HTTP_404_NOT_FOUND")
if request.path.startswith(chat_ui_prefix):
in_ = [url for url in static_dict.get(chat_ui_prefix) if request.path.endswith(url)]
if len(in_) > 0:
a = chat_ui_prefix + in_[0]
return HttpResponseRedirect(a)
index_path = os.path.join(PROJECT_DIR, 'apps', "static", 'chat', 'index.html')
index_path = os.path.join(PROJECT_DIR, "apps", "static", "chat", "index.html")
content = get_index_html(index_path)
content = content.replace("prefix: '/chat'", f"prefix: '{CONFIG.get_chat_path()}'")
content = to_absolute_asset_path(content, chat_ui_prefix)
if not os.path.exists(index_path):
return HttpResponse("页面不存在", status=404)
return HttpResponse(content, status=200)
Expand All @@ -128,15 +151,17 @@ def page_not_found(request, exception):
if len(in_) > 0:
a = admin_ui_prefix + in_[0]
return HttpResponseRedirect(a)
index_path = os.path.join(PROJECT_DIR, 'apps', "static", 'admin', 'index.html')
index_path = os.path.join(PROJECT_DIR, "apps", "static", "admin", "index.html")
if not os.path.exists(index_path):
return HttpResponse("页面不存在", status=404)
content = get_index_html(index_path)
content = content.replace("prefix: '/admin'", f"prefix: '{CONFIG.get_admin_path()}'").replace(
"chatPrefix: '/chat'", f"chatPrefix: '{CONFIG.get_chat_path()}'")
"chatPrefix: '/chat'", f"chatPrefix: '{CONFIG.get_chat_path()}'"
)
content = to_absolute_asset_path(content, admin_ui_prefix)
return HttpResponse(content, status=200)
else:
return HttpResponseRedirect(admin_ui_prefix + '/')
return HttpResponseRedirect(admin_ui_prefix + "/")


handler404 = page_not_found
Loading