web.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. """
  2. URL configuration for maxkb project.
  3. The `urlpatterns` list routes URLs to views. For more information please see:
  4. https://docs.djangoproject.com/en/4.2/topics/http/urls/
  5. Examples:
  6. Function views
  7. 1. Add an import: from my_app import views
  8. 2. Add a URL to urlpatterns: path('', views.home, name='home')
  9. Class-based views
  10. 1. Add an import: from other_app.views import Home
  11. 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
  12. Including another URLconf
  13. 1. Import the include() function: from django.urls import include, path
  14. 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
  15. """
  16. import os
  17. from pathlib import Path
  18. from django.http import HttpResponse, HttpResponseRedirect
  19. from django.urls import path, re_path, include
  20. from django.views import static
  21. from rest_framework import status
  22. from chat.urls import urlpatterns as chat_urlpatterns
  23. from common.init.init_doc import init_doc
  24. from common.result import Result
  25. from maxkb import settings
  26. from maxkb.conf import PROJECT_DIR
  27. from maxkb.const import CONFIG
  28. admin_api_prefix = CONFIG.get_admin_path()[1:] + '/api/'
  29. admin_ui_prefix = CONFIG.get_admin_path()
  30. chat_api_prefix = CONFIG.get_chat_path()[1:] + '/api/'
  31. chat_ui_prefix = CONFIG.get_chat_path()
  32. urlpatterns = [
  33. path(admin_api_prefix, include("users.urls")),
  34. path(admin_api_prefix, include("tools.urls")),
  35. path(admin_api_prefix, include("models_provider.urls")),
  36. path(admin_api_prefix, include("folders.urls")),
  37. path(admin_api_prefix, include("knowledge.urls")),
  38. path(admin_api_prefix, include("system_manage.urls")),
  39. path(admin_api_prefix, include("application.urls")),
  40. path(admin_api_prefix, include("trigger.urls")),
  41. path(admin_api_prefix, include("oss.urls")),
  42. path(chat_api_prefix, include("oss.urls")),
  43. path(chat_api_prefix, include("chat.urls")),
  44. path(f'{admin_ui_prefix[1:]}/', include('oss.retrieval_urls')),
  45. path(f'{chat_ui_prefix[1:]}/', include('oss.retrieval_urls')),
  46. ]
  47. init_doc(urlpatterns, chat_urlpatterns)
  48. def pro():
  49. urlpatterns.append(
  50. re_path(rf'^{CONFIG.get_admin_path()[1:]}/api-doc/(?P<path>.*)$', static.serve,
  51. {'document_root': os.path.join(settings.STATIC_ROOT, "drf_spectacular_sidecar")}, name='doc'),
  52. )
  53. urlpatterns.append(
  54. re_path(rf'^{CONFIG.get_chat_path()[1:]}/api-doc/(?P<path>.*)$', static.serve,
  55. {'document_root': os.path.join(settings.STATIC_ROOT, "drf_spectacular_sidecar")}, name='doc_chat'),
  56. )
  57. # 暴露ui静态资源
  58. urlpatterns.append(
  59. re_path(rf"^{CONFIG.get_admin_path()[1:]}/(?P<path>.*)$", static.serve,
  60. {'document_root': os.path.join(settings.STATIC_ROOT, "admin")},
  61. name='admin'),
  62. )
  63. # 暴露ui静态资源
  64. urlpatterns.append(
  65. re_path(rf'^{CONFIG.get_chat_path()[1:]}/(?P<path>.*)$', static.serve,
  66. {'document_root': os.path.join(settings.STATIC_ROOT, "chat")},
  67. name='chat'),
  68. )
  69. if not settings.DEBUG:
  70. pro()
  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. def get_all_files(directory):
  77. base_path = Path(directory)
  78. file_paths = [
  79. '/' + str(file.relative_to(base_path)).replace('\\', '/')
  80. for file in base_path.rglob('*')
  81. if file.is_file()
  82. ]
  83. return sorted(file_paths, key=len, reverse=True)
  84. static_dict = {
  85. chat_ui_prefix: get_all_files(os.path.join(PROJECT_DIR, 'apps', "static", 'chat')),
  86. admin_ui_prefix: get_all_files(os.path.join(PROJECT_DIR, 'apps', "static", 'admin'))
  87. }
  88. def page_not_found(request, exception):
  89. """
  90. 页面不存在处理
  91. """
  92. if request.path.startswith(admin_ui_prefix + '/api/'):
  93. return Result(response_status=status.HTTP_404_NOT_FOUND, code=404, message="HTTP_404_NOT_FOUND")
  94. if request.path.startswith(chat_ui_prefix + '/api/'):
  95. return Result(response_status=status.HTTP_404_NOT_FOUND, code=404, message="HTTP_404_NOT_FOUND")
  96. if request.path.startswith(chat_ui_prefix):
  97. in_ = [url for url in static_dict.get(chat_ui_prefix) if request.path.endswith(url)]
  98. if len(in_) > 0:
  99. a = chat_ui_prefix + in_[0]
  100. return HttpResponseRedirect(a)
  101. index_path = os.path.join(PROJECT_DIR, 'apps', "static", 'chat', 'index.html')
  102. content = get_index_html(index_path)
  103. content = content.replace("prefix: '/chat'", f"prefix: '{CONFIG.get_chat_path()}'")
  104. if not os.path.exists(index_path):
  105. return HttpResponse("页面不存在", status=404)
  106. return HttpResponse(content, status=200)
  107. elif request.path.startswith(admin_ui_prefix):
  108. in_ = [url for url in static_dict.get(admin_ui_prefix) if request.path.endswith(url)]
  109. if len(in_) > 0:
  110. a = admin_ui_prefix + in_[0]
  111. return HttpResponseRedirect(a)
  112. index_path = os.path.join(PROJECT_DIR, 'apps', "static", 'admin', 'index.html')
  113. if not os.path.exists(index_path):
  114. return HttpResponse("页面不存在", status=404)
  115. content = get_index_html(index_path)
  116. content = content.replace("prefix: '/admin'", f"prefix: '{CONFIG.get_admin_path()}'").replace(
  117. "chatPrefix: '/chat'", f"chatPrefix: '{CONFIG.get_chat_path()}'")
  118. return HttpResponse(content, status=200)
  119. else:
  120. return HttpResponseRedirect(admin_ui_prefix + '/')
  121. handler404 = page_not_found