web.py 7.7 KB

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