web.py 7.8 KB

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