Przeglądaj źródła

Fix: 修复前端路径问题

iaun 2 miesięcy temu
rodzic
commit
aa726f9fb4
1 zmienionych plików z 10 dodań i 6 usunięć
  1. 10 6
      backend/app/__init__.py

+ 10 - 6
backend/app/__init__.py

@@ -42,17 +42,21 @@ def create_app(config_name=None):
     register_error_handlers(app)
     register_error_handlers(app)
     
     
     # Serve frontend static files (for Docker deployment)
     # Serve frontend static files (for Docker deployment)
+    # This must be registered AFTER error handlers to properly catch SPA routes
     if app.static_folder and os.path.exists(app.static_folder):
     if app.static_folder and os.path.exists(app.static_folder):
         @app.route('/')
         @app.route('/')
         def serve_index():
         def serve_index():
             return send_from_directory(app.static_folder, 'index.html')
             return send_from_directory(app.static_folder, 'index.html')
         
         
-        @app.route('/<path:path>')
-        def serve_static(path):
-            # If file exists, serve it; otherwise serve index.html for SPA routing
-            file_path = os.path.join(app.static_folder, path)
-            if os.path.exists(file_path) and os.path.isfile(file_path):
-                return send_from_directory(app.static_folder, path)
+        # Catch-all route for SPA - must be the last route registered
+        @app.errorhandler(404)
+        def serve_spa(error):
+            # Check if this is an API request
+            from flask import request
+            if request.path.startswith('/api/'):
+                # Return JSON error for API requests
+                return {'error': {'code': 'RESOURCE_NOT_FOUND', 'message': 'Resource not found'}}, 404
+            # For non-API requests, serve index.html for SPA routing
             return send_from_directory(app.static_folder, 'index.html')
             return send_from_directory(app.static_folder, 'index.html')
     
     
     return app
     return app