UnisKB/apps/maxkb/urls/web.py

140 lines
5.4 KiB
Python
Raw Normal View History

2025-04-14 12:11:23 +00:00
"""
URL configuration for maxkb project.
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
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'))
"""
2025-06-13 09:35:25 +00:00
import os
2025-07-01 12:10:47 +00:00
from pathlib import Path
2025-06-13 09:35:25 +00:00
2025-07-01 12:10:47 +00:00
from django.http import HttpResponse, HttpResponseRedirect
2025-04-14 12:11:23 +00:00
from django.urls import path, re_path, include
from django.views import static
2025-06-13 09:35:25 +00:00
from rest_framework import status
2025-04-14 12:11:23 +00:00
2025-07-12 09:54:37 +00:00
from chat.urls import urlpatterns as chat_urlpatterns
from common.init.init_doc import init_doc
2025-06-13 09:35:25 +00:00
from common.result import Result
2025-04-14 12:11:23 +00:00
from maxkb import settings
2025-06-13 09:35:25 +00:00
from maxkb.conf import PROJECT_DIR
2025-07-01 07:43:00 +00:00
from maxkb.const import CONFIG
2025-04-14 12:11:23 +00:00
2025-07-01 07:43:00 +00:00
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_ui_prefix = CONFIG.get_chat_path()
2025-04-14 12:11:23 +00:00
urlpatterns = [
2025-07-01 07:43:00 +00:00
path(admin_api_prefix, include("users.urls")),
path(admin_api_prefix, include("tools.urls")),
path(admin_api_prefix, include("models_provider.urls")),
path(admin_api_prefix, include("folders.urls")),
path(admin_api_prefix, include("knowledge.urls")),
path(admin_api_prefix, include("system_manage.urls")),
path(admin_api_prefix, include("application.urls")),
2025-07-02 13:02:45 +00:00
path(admin_api_prefix, include("oss.urls")),
2025-07-04 11:52:45 +00:00
path(chat_api_prefix, include("oss.urls")),
2025-07-01 07:43:00 +00:00
path(chat_api_prefix, include("chat.urls")),
2025-07-02 13:02:45 +00:00
path(f'{admin_ui_prefix[1:]}/', include('oss.retrieval_urls')),
path(f'{chat_ui_prefix[1:]}/', include('oss.retrieval_urls')),
2025-04-14 12:11:23 +00:00
]
2025-07-12 09:54:37 +00:00
init_doc(urlpatterns, chat_urlpatterns)
2025-06-13 09:35:25 +00:00
def pro():
urlpatterns.append(
2025-09-22 03:51:59 +00:00
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'),
2025-06-13 09:35:25 +00:00
)
2025-09-22 03:51:59 +00:00
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'),
)
2025-06-13 09:35:25 +00:00
# 暴露ui静态资源
urlpatterns.append(
2025-07-01 07:43:00 +00:00
re_path(rf"^{CONFIG.get_admin_path()[1:]}/(?P<path>.*)$", static.serve,
{'document_root': os.path.join(settings.STATIC_ROOT, "admin")},
name='admin'),
2025-06-13 09:35:25 +00:00
)
# 暴露ui静态资源
urlpatterns.append(
2025-07-01 07:43:00 +00:00
re_path(rf'^{CONFIG.get_chat_path()[1:]}/(?P<path>.*)$', static.serve,
{'document_root': os.path.join(settings.STATIC_ROOT, "chat")},
2025-06-13 09:35:25 +00:00
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
2025-07-01 12:10:47 +00:00
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()
]
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'))
}
2025-06-13 09:35:25 +00:00
def page_not_found(request, exception):
"""
页面不存在处理
"""
2025-07-01 07:43:00 +00:00
if request.path.startswith(admin_ui_prefix + '/api/'):
2025-06-13 09:35:25 +00:00
return Result(response_status=status.HTTP_404_NOT_FOUND, code=404, message="HTTP_404_NOT_FOUND")
2025-07-01 07:43:00 +00:00
if request.path.startswith(chat_ui_prefix + '/api/'):
2025-06-13 09:35:25 +00:00
return Result(response_status=status.HTTP_404_NOT_FOUND, code=404, message="HTTP_404_NOT_FOUND")
2025-07-01 07:43:00 +00:00
if request.path.startswith(chat_ui_prefix):
2025-07-01 12:10:47 +00:00
in_ = [url for url in static_dict.get(chat_ui_prefix) if request.path.endswith(url)]
if len(in_) > 0:
2025-07-14 03:07:53 +00:00
a = chat_ui_prefix + in_[0]
return HttpResponseRedirect(a)
2025-06-13 09:35:25 +00:00
index_path = os.path.join(PROJECT_DIR, 'apps', "static", 'chat', 'index.html')
2025-07-01 07:43:00 +00:00
content = get_index_html(index_path)
content = content.replace("prefix: '/chat'", f"prefix: '{CONFIG.get_chat_path()}'")
2025-07-01 07:43:00 +00:00
if not os.path.exists(index_path):
return HttpResponse("页面不存在", status=404)
return HttpResponse(content, status=200)
elif request.path.startswith(admin_ui_prefix):
2025-07-01 12:10:47 +00:00
in_ = [url for url in static_dict.get(admin_ui_prefix) if request.path.endswith(url)]
if len(in_) > 0:
a = admin_ui_prefix + in_[0]
return HttpResponseRedirect(a)
2025-07-01 07:43:00 +00:00
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)
2025-07-03 04:26:20 +00:00
content = content.replace("prefix: '/admin'", f"prefix: '{CONFIG.get_admin_path()}'").replace(
2025-07-03 06:34:41 +00:00
"chatPrefix: '/chat'", f"chatPrefix: '{CONFIG.get_chat_path()}'")
2025-07-01 07:43:00 +00:00
return HttpResponse(content, status=200)
2025-06-13 09:35:25 +00:00
else:
2025-07-01 12:10:47 +00:00
return HttpResponseRedirect(admin_ui_prefix + '/')
2025-06-13 09:35:25 +00:00
handler404 = page_not_found