danielle2035 commited on
Commit
ccae231
·
1 Parent(s): fbe61df

Fix: Serve React app as static file instead of Django template

Browse files
Files changed (1) hide show
  1. backend/api/api/urls.py +13 -7
backend/api/api/urls.py CHANGED
@@ -2,20 +2,26 @@ from django.contrib import admin
2
  from django.urls import path, include, re_path
3
  from django.conf.urls.static import static
4
  from django.conf import settings
 
5
  from django.views.generic import TemplateView
6
- from django.http import JsonResponse
7
  from rest_framework import permissions
8
  from drf_yasg.views import get_schema_view
9
  from drf_yasg import openapi
 
10
 
11
  # Health check view
12
  def health_check(request):
13
  """Simple health check endpoint for Hugging Face"""
14
- return JsonResponse({
15
- 'status': 'healthy',
16
- 'service': 'Intel Image Classifier API',
17
- 'version': '1.0.0'
18
- })
 
 
 
 
 
19
 
20
  schema_view = get_schema_view(
21
  openapi.Info(
@@ -37,7 +43,7 @@ urlpatterns = [
37
  path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
38
  path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
39
  # Serve React frontend - catch-all for React Router
40
- re_path(r'^(?!api|admin|swagger|redoc|static|media|health).*$', TemplateView.as_view(template_name='frontend/index.html')),
41
  ]
42
 
43
  urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
 
2
  from django.urls import path, include, re_path
3
  from django.conf.urls.static import static
4
  from django.conf import settings
5
+ from django.http import HttpResponse
6
  from django.views.generic import TemplateView
 
7
  from rest_framework import permissions
8
  from drf_yasg.views import get_schema_view
9
  from drf_yasg import openapi
10
+ import os
11
 
12
  # Health check view
13
  def health_check(request):
14
  """Simple health check endpoint for Hugging Face"""
15
+ return HttpResponse("OK", content_type="text/plain")
16
+
17
+ # React app view - serve the built index.html
18
+ def react_app(request):
19
+ """Serve the React app's index.html"""
20
+ try:
21
+ with open(os.path.join(settings.STATIC_ROOT, 'frontend', 'index.html'), 'r', encoding='utf-8') as f:
22
+ return HttpResponse(f.read(), content_type='text/html')
23
+ except FileNotFoundError:
24
+ return HttpResponse("React app not found. Please check if frontend was built correctly.", status=500)
25
 
26
  schema_view = get_schema_view(
27
  openapi.Info(
 
43
  path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
44
  path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
45
  # Serve React frontend - catch-all for React Router
46
+ re_path(r'^(?!api|admin|swagger|redoc|static|media|health).*$', react_app),
47
  ]
48
 
49
  urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)