web.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. builder_api_prefix = CONFIG.get_builder_path()[1:] + '/api/'
  33. builder_ui_prefix = CONFIG.get_builder_path()
  34. urlpatterns = [
  35. path(admin_api_prefix, include("users.urls")),
  36. path(admin_api_prefix, include("system_manage.urls")),
  37. path(admin_api_prefix, include("oss.urls")),
  38. path(builder_api_prefix, include("tools.urls")),
  39. path(builder_api_prefix, include("models_provider.urls")),
  40. path(builder_api_prefix, include("folders.urls")),
  41. path(builder_api_prefix, include("knowledge.urls")),
  42. path(builder_api_prefix, include("application.urls")),
  43. path(builder_api_prefix, include("trigger.urls")),
  44. path(builder_api_prefix, include("oss.urls")),
  45. path(chat_api_prefix, include("oss.urls")),
  46. path(chat_api_prefix, include("chat.urls")),
  47. path(f'{admin_ui_prefix[1:]}/', include('oss.retrieval_urls')),
  48. path(f'{builder_ui_prefix[1:]}/', include('oss.retrieval_urls')),
  49. path(f'{chat_ui_prefix[1:]}/', include('oss.retrieval_urls')),
  50. ]
  51. init_doc(urlpatterns, chat_urlpatterns)
  52. def pro():
  53. urlpatterns.append(
  54. re_path(rf'^{CONFIG.get_admin_path()[1:]}/api-doc/(?P<path>.*)$', static.serve,
  55. {'document_root': os.path.join(settings.STATIC_ROOT, "drf_spectacular_sidecar")}, name='doc'),
  56. )
  57. urlpatterns.append(
  58. re_path(rf'^{CONFIG.get_chat_path()[1:]}/api-doc/(?P<path>.*)$', static.serve,
  59. {'document_root': os.path.join(settings.STATIC_ROOT, "drf_spectacular_sidecar")}, name='doc_chat'),
  60. )
  61. urlpatterns.append(
  62. re_path(rf'^{CONFIG.get_builder_path()[1:]}/api-doc/(?P<path>.*)$', static.serve,
  63. {'document_root': os.path.join(settings.STATIC_ROOT, "drf_spectacular_sidecar")}, name='doc_builder'),
  64. )
  65. # 暴露ui静态资源
  66. urlpatterns.append(
  67. re_path(rf"^{CONFIG.get_admin_path()[1:]}/(?P<path>.*)$", static.serve,
  68. {'document_root': os.path.join(settings.STATIC_ROOT, "admin")},
  69. name='admin'),
  70. )
  71. # 暴露ui静态资源
  72. urlpatterns.append(
  73. re_path(rf'^{CONFIG.get_chat_path()[1:]}/(?P<path>.*)$', static.serve,
  74. {'document_root': os.path.join(settings.STATIC_ROOT, "chat")},
  75. name='chat'),
  76. )
  77. # 暴露builder静态资源
  78. urlpatterns.append(
  79. re_path(rf'^{CONFIG.get_builder_path()[1:]}/(?P<path>.*)$', static.serve,
  80. {'document_root': os.path.join(settings.STATIC_ROOT, "builder")},
  81. name='builder'),
  82. )
  83. if not settings.DEBUG:
  84. pro()
  85. def get_index_html(index_path):
  86. file = open(index_path, "r", encoding='utf-8')
  87. content = file.read()
  88. file.close()
  89. return content
  90. def get_all_files(directory):
  91. base_path = Path(directory)
  92. file_paths = [
  93. '/' + str(file.relative_to(base_path)).replace('\\', '/')
  94. for file in base_path.rglob('*')
  95. if file.is_file()
  96. ]
  97. return sorted(file_paths, key=len, reverse=True)
  98. static_dict = {
  99. chat_ui_prefix: get_all_files(os.path.join(PROJECT_DIR, 'apps', "static", 'chat')),
  100. admin_ui_prefix: get_all_files(os.path.join(PROJECT_DIR, 'apps', "static", 'admin')),
  101. builder_ui_prefix: get_all_files(os.path.join(PROJECT_DIR, 'apps', "static", 'builder')),
  102. }
  103. def page_not_found(request, exception):
  104. """
  105. 页面不存在处理
  106. """
  107. if request.path.startswith(admin_ui_prefix + '/api/'):
  108. return Result(response_status=status.HTTP_404_NOT_FOUND, code=404, message="HTTP_404_NOT_FOUND")
  109. if request.path.startswith(chat_ui_prefix + '/api/'):
  110. return Result(response_status=status.HTTP_404_NOT_FOUND, code=404, message="HTTP_404_NOT_FOUND")
  111. if request.path.startswith(builder_ui_prefix + '/api/'):
  112. return Result(response_status=status.HTTP_404_NOT_FOUND, code=404, message="HTTP_404_NOT_FOUND")
  113. if request.path.startswith(chat_ui_prefix):
  114. in_ = [url for url in static_dict.get(chat_ui_prefix) if request.path.endswith(url)]
  115. if len(in_) > 0:
  116. a = chat_ui_prefix + in_[0]
  117. return HttpResponseRedirect(a)
  118. index_path = os.path.join(PROJECT_DIR, 'apps', "static", 'chat', 'index.html')
  119. content = get_index_html(index_path)
  120. content = content.replace("prefix: '/chat'", f"prefix: '{CONFIG.get_chat_path()}'")
  121. if not os.path.exists(index_path):
  122. return HttpResponse("页面不存在", status=404)
  123. return HttpResponse(content, status=200)
  124. elif request.path.startswith(builder_ui_prefix):
  125. in_ = [url for url in static_dict.get(builder_ui_prefix) if request.path.endswith(url)]
  126. if len(in_) > 0:
  127. a = builder_ui_prefix + in_[0]
  128. return HttpResponseRedirect(a)
  129. index_path = os.path.join(PROJECT_DIR, 'apps', "static", 'builder', 'index.html')
  130. content = get_index_html(index_path)
  131. content = content.replace("prefix: '/builder'", f"prefix: '{CONFIG.get_builder_path()}'")
  132. if not os.path.exists(index_path):
  133. return HttpResponse("页面不存在", status=404)
  134. return HttpResponse(content, status=200)
  135. elif request.path.startswith(admin_ui_prefix):
  136. in_ = [url for url in static_dict.get(admin_ui_prefix) if request.path.endswith(url)]
  137. if len(in_) > 0:
  138. a = admin_ui_prefix + in_[0]
  139. return HttpResponseRedirect(a)
  140. index_path = os.path.join(PROJECT_DIR, 'apps', "static", 'admin', 'index.html')
  141. if not os.path.exists(index_path):
  142. return HttpResponse("页面不存在", status=404)
  143. content = get_index_html(index_path)
  144. content = content.replace("prefix: '/admin'", f"prefix: '{CONFIG.get_admin_path()}'").replace(
  145. "builderPrefix: '/builder'", f"builderPrefix: '{CONFIG.get_builder_path()}'").replace(
  146. "chatPrefix: '/chat'", f"chatPrefix: '{CONFIG.get_chat_path()}'")
  147. return HttpResponse(content, status=200)
  148. else:
  149. return HttpResponseRedirect(admin_ui_prefix + '/')
  150. handler404 = page_not_found