|
14 | 14 | 1. Import the include() function: from django.urls import include, path |
15 | 15 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) |
16 | 16 | """ |
| 17 | +import os |
| 18 | + |
| 19 | +from django.http import HttpResponse |
17 | 20 | from django.urls import path, re_path, include |
18 | 21 | from django.views import static |
19 | 22 | from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView |
| 23 | +from rest_framework import status |
20 | 24 |
|
| 25 | +from common.result import Result |
21 | 26 | from maxkb import settings |
| 27 | +from maxkb.conf import PROJECT_DIR |
22 | 28 |
|
23 | 29 | urlpatterns = [ |
24 | 30 | path("api/", include("users.urls")), |
|
28 | 34 | path("api/", include("knowledge.urls")), |
29 | 35 | path("api/", include("system_manage.urls")), |
30 | 36 | path("api/", include("application.urls")), |
31 | | - path("chat/", include("chat.urls")), |
| 37 | + path("chat/api/", include("chat.urls")), |
32 | 38 | path('oss/', include('oss.urls')), |
33 | 39 | ] |
34 | 40 | urlpatterns += [ |
|
39 | 45 | urlpatterns.append( |
40 | 46 | re_path(r'^static/(?P<path>.*)$', static.serve, {'document_root': settings.STATIC_ROOT}, name='static'), |
41 | 47 | ) |
| 48 | + |
| 49 | + |
| 50 | +def pro(): |
| 51 | + # 暴露静态主要是swagger资源 |
| 52 | + urlpatterns.append( |
| 53 | + re_path(r'^static/(?P<path>.*)$', static.serve, {'document_root': settings.STATIC_ROOT}, name='static'), |
| 54 | + ) |
| 55 | + # 暴露ui静态资源 |
| 56 | + urlpatterns.append( |
| 57 | + re_path(r'^ui/(?P<path>.*)$', static.serve, {'document_root': os.path.join(settings.STATIC_ROOT, "ui")}, |
| 58 | + name='ui'), |
| 59 | + ) |
| 60 | + # 暴露ui静态资源 |
| 61 | + urlpatterns.append( |
| 62 | + re_path(r'^chat/(?P<path>.*)$', static.serve, {'document_root': os.path.join(settings.STATIC_ROOT, "chat")}, |
| 63 | + name='chat'), |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +if not settings.DEBUG: |
| 68 | + pro() |
| 69 | + |
| 70 | + |
| 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 | + |
| 77 | + |
| 78 | +def page_not_found(request, exception): |
| 79 | + """ |
| 80 | + 页面不存在处理 |
| 81 | + """ |
| 82 | + if request.path.startswith("/api/"): |
| 83 | + return Result(response_status=status.HTTP_404_NOT_FOUND, code=404, message="HTTP_404_NOT_FOUND") |
| 84 | + if request.path.startswith("/chat/api/"): |
| 85 | + return Result(response_status=status.HTTP_404_NOT_FOUND, code=404, message="HTTP_404_NOT_FOUND") |
| 86 | + if request.path.startswith('/chat'): |
| 87 | + index_path = os.path.join(PROJECT_DIR, 'apps', "static", 'chat', 'index.html') |
| 88 | + else: |
| 89 | + index_path = os.path.join(PROJECT_DIR, 'apps', "static", 'ui', 'index.html') |
| 90 | + if not os.path.exists(index_path): |
| 91 | + return HttpResponse("页面不存在", status=404) |
| 92 | + content = get_index_html(index_path) |
| 93 | + return HttpResponse(content, status=200) |
| 94 | + |
| 95 | + |
| 96 | +handler404 = page_not_found |
0 commit comments